#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using ll = long long;
using namespace std;

vector<vector<int>>g;
vector<vector<int>>grev;

vector<int>topsort;
vector<bool>used;
void dfs_topsort(int v) {
    used[v] = 1;
    for (int u : g[v]) {
        if (!used[u]) dfs_topsort(u);
    }
    topsort.push_back(v);
}

vector<int>color;
void dfs_color(int v, int col) {
    color[v] = col;
    for (int u : grev[v]) {
        if (color[u] == -1) {
            dfs_color(u, col);
        }
    }
}

vector<vector<int>>g1;
vector<int>h;
int ans = 0;
int dfs_path(int v, int hh) {
    h[v] = hh;
    used[v] = 1;
    for (int u : g[v]) {
        if (used[u] == 2) continue;
        if (used[u] == 1) return u;
        int w = dfs_path(u, hh + 1);
        if (w != -1) {
            ans -= h[w] - h[v];
        }
    }
    return -1;
}

int main() {
    int n, m, kkk;
    cin >> n >> m >> kkk;
    g.resize(n);
    grev.resize(n);
    used.resize(n);
    color.resize(n, -1);
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        --u; --v;
        g[u].push_back(v);
        grev[v].push_back(u);
    }
    for (int i = 0; i < n; ++i) {
        if (!used[i])dfs_topsort(i);
    }
    reverse(topsort.begin(), topsort.end());
    int cur = 0;
    for (int x : topsort) {
        if (color[x] == -1) {
            dfs_color(x, cur++);
        }
    }
    vector<int>sz(cur);
    for (int i = 0; i < n; ++i) {
        sz[color[i]]++;
    }
    ll res = 0;
    for (int x : sz) {
        res += max(0, x - 1);
    }
    cout << res << "\n"<<"-1\n";
    

}