import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
// 基础并查集
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static int[] fa = new int[10050];
    static int find(int x) {
        if (x == fa[x]) {
            return x;
        }
        fa[x] = find(fa[x]);
        return fa[x];
    }
    public static void main(String[] args) throws java.io.IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        Scanner in = new Scanner(System.in);
        PrintWriter pw = new PrintWriter(System.out);

        int n, a, m;
        n = in.nextInt();
        a = in.nextInt();
        m = in.nextInt();
        for (int i = 0; i < n; i++) {
            fa[i] = i;
        }
        int ans = -1;
        for (int i = 1 ; i <= m; i++) {
            String s1 = in.next();
            String[] str = s1.split(",");
            int x = Integer.parseInt(str[0]);
            int y = Integer.parseInt(str[1]);
            if(x == a || y == a)
                ans--;
            int nx = find(x);
            int ny = find(y);
            if (nx != ny) {
                fa[nx] = ny;
            }
        }

        int k = find(a);
        for (int i = 0; i < n; i++) {
            if(find(i) == k){
                ans ++;
            }
        }
        pw.print(ans);
        pw.close();
        bf.close();
    }
}