https://www.acmicpc.net/problem/11724
import java.util.*;
public class Main {
static int arr[][];
static boolean check[];
static int n;
static void dfs(int a){
check[a]=true;
for(int i=1;i<=n;i++){
if(arr[a][i]==1 && check[i]==false) dfs(i);
}
}
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
n=s.nextInt();
int m=s.nextInt();
arr=new int[n+1][n+1];
check=new boolean[n+1];
while(m-->0){
int u=s.nextInt();
int v=s.nextInt();
arr[u][v]=arr[v][u]=1;
}
int count=0;
for(int i=1;i<=n;i++){
if(check[i]==false){
dfs(i);
count++;
}
}
System.out.print(count);
}
}