#include #include using namespace std; using namespace atcoder; typedef int64_t lint; #define rep(i, n) for(int i=0; i; using vvi = vector>; template inline void vin(vector& v) { rep(i, v.size()) cin >> v.at(i); } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template inline void drop(T x) { cout << x << endl; exit(0); } template void vout(vector v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; } constexpr lint LINF = LLONG_MAX/2; #define mp make_pair using plint = pair; vector>> G; vector>> R; void make_round_graph(lint N, lint M) { lint x, y; G.resize(N), R.resize(N); rep(i, M) { cin >> x >> y; x--, y--; G[x].push_back(make_pair(1, y)); R[y].push_back(make_pair(1, x)); } } void dijkstra(vector &dist) { lint a=0, b=0, x, y, N = G.size(); dist.resize(N, -1); priority_queue, greater> PQ; rep(i, N) { if (R[i].size() == 0) { dist[i] = 0; PQ.push(make_pair(0, i)); } } while (!PQ.empty()) { tie(x, a) = PQ.top(); PQ.pop(); if (dist[a] > x) continue; for (auto p : G[a]) { tie(y, b) = p; if (chmax(dist[b], x+y)) PQ.push(make_pair(x+y, b)); } } } vi topo_sort() { vi res; lint N = G.size(); vi v(N); rep(i, N) { for (auto e : G[i]) v[e.second]++; } queue Q; rep(i, N) { if (!v[i]) Q.push(i); } while (!Q.empty()) { int x = Q.front(); Q.pop(); res.push_back(x); for (auto e : G[x]) { v[e.second]--; if (!v[e.second]) Q.push(e.second); } } return res; } int main() { lint N, K; cin >> N >> K; make_round_graph(N, K); lint a=0, b=0, c=0, x, y, z; vi v, w; w = topo_sort(); if (w.size() < N) drop(-1); dijkstra(v); rep(i, N) { chmax(a, v[i]); } std::cout << a+1 << '\n'; }