#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
struct SCC {
    vector<vector<int> > SC; int NV; vector<int> GR, rev, NUM, vis; vector<vector<int>> E, RE;
    void init(int NV) { GR.resize(NV); rev.resize(NV); vis.resize(NV); E.resize(NV, vector<int>());
        RE.resize(NV, vector<int>());this->NV = NV;}
    void add_edge(int x, int y) { E[x].push_back(y); RE[y].push_back(x); }
    void dfs(int cu) { vis[cu] = 1;
        for (int i = 0; i<E[cu].size(); i++) if (!vis[E[cu][i]]) dfs(E[cu][i]); NUM.push_back(cu); }
    void revdfs(int cu, int ind) { int i; vis[cu] = 1; GR[cu] = ind; SC[ind].push_back(cu);
        rev[cu] = ind; rep(i, 0, RE[cu].size()) if (!vis[RE[cu][i]]) revdfs(RE[cu][i], ind); }
    void scc() { int c = 0; SC.clear(); SC.resize(NV); NUM.clear();
        rep(i, 0, NV) vis[i] = 0; for (int i = 0; i < NV; i++) if (!vis[i]) dfs(i);
        rep(i, 0, NV) vis[i] = 0; for (int i = NUM.size() - 1; i >= 0; i--) if (!vis[NUM[i]]) {
        SC[c].clear(); revdfs(NUM[i], c); sort(SC[c].begin(), SC[c].end()); c++;} SC.resize(c); }};
struct TopologicalSort {
    vector<set<int>> E; TopologicalSort(int N) { E.resize(N); }
    void add_edge(int a, int b) { E[a].insert(b); }
    bool visit(int v, vector<int>& order, vector<int>& color) { color[v] = 1;
        for (int u : E[v]) { if (color[u] == 2) continue; if (color[u] == 1) return false;
        if (!visit(u, order, color)) return false; } order.push_back(v); color[v] = 2; return true; }
    bool sort(vector<int> &order) { int n = E.size(); vector<int> color(n);
        for (int u = 0; u < n; u++) if (!color[u] && !visit(u, order, color)) return false;
        reverse(order.begin(), order.end()); return true; } };
struct SCCtoDAG {
    int N = -1; vector<vector<int>> E; int M; vector<int> mapping; vector<set<int>> newE;
    vector<int> tsorder; map<int, vector<int>> group;
    void init(int n) { N = n; E.resize(N, vector<int>()); }
    void add_edge(int a, int b) {assert(0 < N && "Not Init!!");E[a].push_back(b);}
    void build() {SCC scc;scc.init(N);rep(i, 0, N) for(int j : E[i]) scc.add_edge(i, j);scc.scc();
        vector<int> dec;rep(i, 0, N) dec.push_back(scc.GR[i]);sort(dec.begin(), dec.end());
        dec.erase(unique(dec.begin(), dec.end()), dec.end());M = dec.size();mapping.clear();
        rep(i, 0, N) {int id = lower_bound(dec.begin(), dec.end(), scc.GR[i]) - dec.begin();
        mapping.push_back(id);}newE.resize(M, set<int>());rep(i, 0, N) for (int j : E[i]) {
        int ii = mapping[i], jj = mapping[j];if (ii == jj) continue;newE[ii].insert(jj);}
        TopologicalSort ts(M);rep(i, 0, M) for(int j : newE[i]) ts.add_edge(i, j);
        tsorder.clear();ts.sort(tsorder); rep(i, 0, N) group[mapping[i]].push_back(i); }};
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　 |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/






int N, M;
SCCtoDAG scctodag;
int inedge[101010], outedge[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N >> M;
    scctodag.init(N);
    rep(i, 0, M) {
        int s, t; cin >> s >> t;
        scctodag.add_edge(s, t);
    }
    scctodag.build();

    if (scctodag.M == 1) {
        printf("0\n");
        return;
    }

    int source = 0, sink = 0;
    rep(cu, 0, scctodag.M) {
        fore(to, scctodag.newE[cu]) {
            outedge[cu]++;
            inedge[to]++;
        }
    }
    rep(cu, 0, scctodag.M) {
        if (outedge[cu] == 0) sink++;
        if (inedge[cu] == 0) source++;
    }

    int ans = max(source, sink);
    cout << ans << endl;
}


Measured memory_bytes: 17712