public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		for (int i = 0; i < n; i++) {
			start.add(new Node(i));
			finish.add(new Node(i));
		}

		int m = s.nextInt();  //количество рёбер

		if (m == 0) {
			System.out.println(n);
			return;
		}
		while (m-- > 0) {
			int i = s.nextInt() - 1;
			int j = s.nextInt() - 1;
			start.get(i).add(start.get(j));
			finish.get(j).add(finish.get(i));

		}

		Node[][] temp = cutDrains();
		nodes1 = temp[0];
		Node[] nodes2 = temp[1];

		//System.out.println(Arrays.toString(nodes1));
		for (Node z : nodes1) {
			if (z.flag == 0) {
				DFS(nodes1, z.id);
			}
		}
		Arrays.sort(nodes1, new Comparator<Node>() {
			@Override
			public int compare(Node o1, Node o2) {
				return -o1.end + o2.end;
			}
		});
		//System.out.println(Arrays.toString(nodes1));

		for (int i = 0; i < nodes1.length; i++) {
			nodes2[nodes1[i].id].id = i;
		}
		Arrays.sort(nodes2, new Comparator<Node>() {
			@Override
			public int compare(Node o1, Node o2) {
				return +o1.id - o2.id;
			}
		});
		//System.out.println(Arrays.toString(nodes2));

		for (Node z : nodes2) {
			if (z.flag == 0) {
				DFS(nodes2, z.id);
				result++;
			}
		}
		System.out.println(result);
//----------------------------------------------
	}