public static void main(String[] args) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder out = new StringBuilder();
		String line = "";
		while ((line = in.readLine()) != null && line.length() != 0) {
			String[] info = line.trim().split(" ");
			int n = Integer.parseInt(info[0]);
			int m = Integer.parseInt(info[1]);
			if (n == 0 && m == 0)
				break;
			Tarjan t = new Tarjan(n);
			HashMap<String, Integer> names = new HashMap<>();
			int id = 0;
			for (int j = 0; j < n; j++) {
				String name = in.readLine().trim();
				if (!names.containsKey(name))
					names.put(name, id++);
			}
			for (int j = 0; j < m; j++) {
				int a = names.get(in.readLine().trim());
				int b = names.get(in.readLine().trim());
				t.add(a, b);
			}
			t.SCC();
			out.append(t.numSCC + "\n");
		}
		System.out.print(out);
	}