#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;
#define int long long
constexpr int INF = 1e18;
constexpr int MOD = 1e9 + 7;
//mt19937_64 rnd(0);

vector<vector<int>> g, gr, kond;
vector<int> order, num;
vector<int> used;
int c = 0, cnt = 0;

void topsort(int v) {
    used[v] = c;
    for (auto to : gr[v]) {
        if (used[to] != c) {
            topsort(to);
        }
    }
    order.push_back(v);
}

int build_kss(int v) {
    used[order[v]] = c;
    int mx = v;
    for (auto to : g[v]) {
        if (used[to] != c) {
            mx = max(mx, build_kss(num[to]));
        }
    }
    return mx;
}

void dfs2(int v) {
    used[v] = c;
    for (auto to : kond[v]) {
        if (used[to] != c) {
            dfs2(to);
        } else {
            cnt++;
        }
    }
}

void solve() {
    int n, m, group;
    cin >> n >> m >> group;
    g.assign(n, {});
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        gr[v].push_back(u);
    }
    c++;
    for (int i = 0; i < n; ++i) {
        if (used[i] != c) {
            topsort(i);
        }
    }
    reverse(order.begin(), order.end());
    num.assign(n, {});
    for (int i = 0; i < n; ++i) {
        num[order[i]] = i;
    }
    vector<pair<int, int>> kss;
    c++;
    int i = 0;
    while (i < n) {
        int x = build_kss(i);
        kss.push_back({i, x});
        i = x + 1;
    }
    kond.assign(kss.size(), {});
    int j = 0;
    for (int i = 0; i < n; ++i) {
        if (i > kss[j].second) {
            j++;
        }
        for (auto to : g[i]) {
            if (num[to] < kss[j].first) {
                int x = upper_bound(kss.begin(), kss.end(), make_pair(num[to], INF)) - kss.begin() - 1;
                kond[j].push_back(x);
            }
        }
    }
    c++;
    for (int i = 0; i < kss.size(); ++i) {
        if (used[i] != c) {
            dfs2(i);
        }
    }
    cout << cnt << "\n";
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
#ifdef MULTY
    cin >> t;
#endif
    while (t--) {
        solve();
    }

    return 0;
}