#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
#ifdef home
    freopen("f.in", "r", stdin);
    freopen("f.out", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, usls;
    cin >> n >> m >> usls;
    vector<vector<int>> g(n);
    for (int i = 0; i < m; i++) {
        int v, u;
        cin >> v >> u;
        v--, u--;
        g[v].emplace_back(u);
    }
    int cnt = 0;
    vector<bool> used(n);
    auto dfs = [&](auto &&self, int v) -> void {
        used[v] = true;
        for (auto &u: g[v]) {
            if (used[u]) {
                cnt++;
            } else {
                self(self, u);
            }
        }
    };
    for (int v = 0; v < n; v++) {
        if (!used[v]) {
            dfs(dfs, v);
        }
    }
    cout << cnt << '\n';
    cout << "-1\n";
    return 0;
}
