#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define str string
#define pb push_back
#define ins insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pii pair<int, int>
#define ff first
#define ss second
#define ld long double
#define sz(a) (int)a.size()

mt19937 rnd(1488);

vector<vector<int>> gr;
vector<vector<int>> g;
vector<vector<int>> gr2;
vector<bool> fl;
vector<int> eu;
vector<int> cnt;
vector<int> cv;
int col = 0;
int ans = 0;

void dfs(int v){
    eu.pb(v);
    fl[v] = true;
    for (auto u:gr[v]){
        if (!fl[u]){
            dfs(u);
        }
    }
}

void dfs2(int v){
    cv[v] = col;
    fl[v] = true;
    for (auto u:g[v]){
        if (!fl[u]){
            dfs2(u);
        }
    }
}

void dfs3(int v){
    fl[v] = true;
    for (auto u:gr2[v]){
        if (!fl[u]){
            dfs3(u);
        }
        else{
            ans++;
        }
    }
}

void solve(){
    int n, m, g2;
    cin >> n >> m >> g2;
    gr.resize(n);
    g.resize(n);
    vector<pii> r;
    for (int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        --u;
        --v;
        r.pb({u, v});
        gr[u].pb(v);
        g[v].pb(u);
    }
    fl.resize(n);
    for (int i = 0; i < n; i++){
        if (!fl[i]){
            dfs(0);
        }
    }
    fl.assign(n, false);
    cnt.resize(n);
    cv.resize(n);
    for (auto u:eu){
        if (!fl[u]){
            dfs2(u);
            col++;
        }
    }
    gr2.resize(col);
    for (auto [u, v]:r){
        if (cv[u] != cv[v]){
            gr2[cv[u]].pb(cv[v]);
        }
        else{
            cnt[cv[u]]++;
        }
    }
    for (int i = 0; i < col; i++){
        ans += (cnt[i] + 1) / 2;
    }
    fl.assign(col, false);
    for (int i = 0; i < col; i++){
        if (!fl[i]){
            dfs3(i);
        }
    }
    cout << ans << "\n-1\n";
}

int32_t main(){
    cin.tie(0)->ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("inp.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif // LOCAL
    int _ = 1;
    //cin >> _;
    while (_--){
        solve();
    }
}
