// Arup Guha
// 2/3/2018
// Solution to 2017 MCPC Problem G: Faulty Robot

import java.util.*;

public class faultyrobot {

	public static int[] forced;
	public static ArrayList[] rest;

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		int numE = stdin.nextInt();

		// -1 indicates no forced edge.
		forced = new int[n];
		Arrays.fill(forced, -1);

		// For the regular graph.
		rest = new ArrayList[n];
		for (int i=0; i<n; i++) rest[i] = new ArrayList<Integer>();

		// Go through each edge.
		for (int i=0; i<numE; i++) {

			// Did it this way so I can retain negative value of v1 initially.
			int v1 = stdin.nextInt();
			int v2 = stdin.nextInt()-1;

			if (v1 < 0)
				forced[-v1-1] = v2;
			else
				rest[v1-1].add(v2);
		}

		// We start at node 0.
		LinkedList<Integer> q = new LinkedList<Integer>();
		q.offer(0);

		// 0-(n-1) is w/o buggy, n-(2n-1) is with buggy.
		boolean[] visited = new boolean[2*n];
		visited[0] = true;

		// Mark where we can end.
		boolean[] canEnd = new boolean[n];

		// Yeah for BFS...
		while (q.size() > 0) {

			int cur = q.poll();

			// Can do buggy move still.
			if (cur < n) {

				// Could end here OR do a buggy move.

				// Mark this as a possible ending spot.
				if (forced[cur] == -1) canEnd[cur] = true;

				// Forced move.
				else {
					int next = forced[cur];
					if (!visited[next]) {
						q.offer(next);
						visited[next] = true;
					}
				}

				// We could always do a buggy move though.
				for (Integer v : (ArrayList<Integer>)rest[cur]) {
					int next = v + n;
					if (!visited[next]) {
						q.offer(next);
						visited[next] = true;
					}
				}
			}

			// Must follow directions.
			else {

				// No forces we could end here.
				if (forced[cur-n] == -1) {
					canEnd[cur-n] = true;
				}

				// There is a force and we must take it.
				else {
					int next = n + forced[cur-n];
					if (!visited[next]) {
						q.offer(next);
						visited[next] = true;
					}
				}
			}
		}

		// Count them up.
		int res = 0;
		for (int i=0; i<n; i++)
			if (canEnd[i])
				res++;
		System.out.println(res);

	}
}