#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);
        }
    }
}
vector<int>ts;
void tipsort(int i){
    u[i]=1;
    for (auto j:a[i]){
        if (u[j]==0){
            tipsort(j);
        }
    }
    ts.push_back(i);
}
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);
    }
    u.resize(n);
    for (int i=0;i<n;++i){
        if (u[i]==0){
            tipsort(i);
        }
    }
    reverse(ts.begin(),ts.end());
    u.clear();
    u.resize(n);
    /*for (auto i:ts){
        cout<<i<<' ';
    }
    ns*/
    vector<int>p(n);
    for (int i=0;i<n;++i){
        p[ts[i]]=i;
    }
   /* for (auto i:p){
        cout<<i<<' ';
    }
    ns*/
    for (int i=0;i<n;++i){
        for (auto j:a[i]){
            b[j].push_back(i);
        }
    }
    int w=0;
    for (auto i:ts){
        if (u[i]==0){
            dfs(i);
            w++;
        }
    }
    cout<<w<<endl<<-1;
}