import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class 실험실 {
static int N;
static int count;
static int answer = 0;
static int[][] arr;
static boolean[] virus;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
N = Integer.parseInt(br.readLine());
count = Integer.parseInt(br.readLine());
arr = new int[count+1][2];
virus = new boolean[N+1];
for(int i = 1 ; i <= count ; i++) {
st = new StringTokenizer(br.readLine());
arr[i][0] = Integer.parseInt(st.nextToken());
arr[i][1] = Integer.parseInt(st.nextToken());
}
dfs(1);
System.out.print(--answer);
}
static void dfs(int start) {
if(!virus[start]) {
virus[start] = true;
answer++;
}else {
return;
}
for(int i = 1 ; i <= count ; i++) {
if(arr[i][0] == start) {
dfs(arr[i][1]);
}else if(arr[i][1] == start){
dfs(arr[i][0]);
}
}
}
}