#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast,unroll-loops")
using namespace std;
#define int long long
#define ns cout << "\n";
#define ld long double
const int INF=1e9+7,N=1e6;
int n,m,g;
vector<vector<int>>a(n);
vector<vector<int>>b;
vector<int>u;
void dfs(int i){
    u[i]=1;
    for (auto j:b[i]){
        if (u[j]==0){
            dfs(j);
        }
    }
}
signed main(){
    cin>>n>>m>>g;
    vector<int>g(n);
    a.resize(n);
    b.resize(n);
    u.resize(n);
    while (m--){
        int l,r;
        cin>>l>>r;
        l--;
        r--;
        g[r]++;
        a[l].push_back(r);
    }
    deque<int>o;
    for (int i=0;i<n;++i){
        if (g[i]==0){
            o.push_back(i);
        }
    }
    vector<int>ts;
    while (!o.empty()){
        int el=o[0];
        o.pop_front();
        ts.push_back(el);
        for (auto j:a[el]){
            g[j]--;
            if (g[j]==0){
                o.push_back(j);
            }
        }
    }
    vector<int>p(n);
    for (int i=0;i<n;++i){
        p[g[i]]=i;
    }
    for (int i=0;i<n;++i){
        for (auto j:a[i]){
            if (p[j]>p[i]){
                b[j].push_back(i);
                b[i].push_back(j);
            }
        }
    }
    int w=0;
    for (int i=0;i<n;++i){
        if (u[i]==0){
            dfs(i);
            w++;
        }
    }
    cout<<w<<endl<<-1;
}