import java.util.*;

public class RagnarTranscribed {
    static int counter, ans, C;
    static int[] n, l, com;
    static List<Integer> st;
    static List<List<Integer>> g;
    static boolean[] vs;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int m = scanner.nextInt();
        g = new ArrayList<>();
        for (int i = 0; i < N; i++) {
            g.add(new ArrayList<>());
        }
        for (int i = 0; i < m; i++) {
            int a = scanner.nextInt() - 1;
            int b = scanner.nextInt() - 1;
            g.get(b).add(a);
        }
        counter = 0;
        n = new int[N];
        l = new int[N];
        Arrays.fill(n, -1);
        Arrays.fill(l, -1);
        vs = new boolean[N];
        st = new ArrayList<>();
        ans = 0;
        com = new int[N];
        Arrays.fill(com, -1);
        C = 0;

        for (int u = 0; u < N; u++) {
            if (n[u] == -1) {
                visit(u);
            }
        }
        System.out.println(ans);
    }

    public static void visit(int u) {
        l[u] = n[u] = counter;
        st.add(u);
        counter += 1;
        vs[u] = true;
        for (int v : g.get(u)) {
            if (n[v] == -1) {
                visit(v);
            }
            if (vs[v]) {
                l[u] = Math.min(l[u], l[v]);
            }
        }
        if (l[u] == n[u]) {
            List<Integer> cc = new ArrayList<>();
            while (true) {
                int v = st.get(st.size() - 1);
                st.remove(st.size() - 1);
                vs[v] = false;
                com[v] = C;
                cc.add(v);
                if (u == v) {
                    break;
                }
            }
            boolean leaf = true;
            for (int v : cc) {
                for (int w : g.get(v)) {
                    if (com[w] != C) {
                        leaf = false;
                    }
                }
            }
            if (leaf) {
                ans += 1;
            }
            C += 1;
        }
    }
}