import java.util.ArrayList; import java.util.Scanner; public class ConnectedComponents { static int numberOfComponents(ArrayList> adj) { ArrayList visited = new ArrayList<>(); int cc = 0; for (int v = 0; v < adj.size(); v++) { if (!visited.contains(v)) { explore(adj, visited, v); cc++; } } return cc; } private static void explore(ArrayList> adj, ArrayList visited, int v) { visited.add(v); for (int n : adj.get(v)) { if (!visited.contains(n)) explore(adj, visited, n); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); ArrayList> adj = new ArrayList>(); for (int i = 0; i < n; i++) { adj.add(new ArrayList()); } for (int i = 0; i < m; i++) { int x, y; x = scanner.nextInt(); y = scanner.nextInt(); adj.get(x - 1).add(y - 1); adj.get(y - 1).add(x - 1); } System.out.println(numberOfComponents(adj)); } }