#include #include using namespace std; using namespace atcoder; #define rep(i,n)for (int i = 0; i < int(n); ++i) #define rrep(i,n)for (int i = int(n)-1; i >= 0; --i) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() template void chmax(T& a, const T& b) {a = max(a, b);} template void chmin(T& a, const T& b) {a = min(a, b);} using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; std::vector topological_sort(const std::vector>& graph) { const int n = graph.size(); std::vector indeg(n); for(int u = 0; u < n; u++) for(auto v: graph[u]) { indeg[v]++; } std::vector todo; todo.reserve(n); for(int u = 0; u < n; u++) if (indeg[u] == 0) todo.push_back(u); std::vector ret; ret.reserve(n); while(!todo.empty()) { int u = todo.back(); todo.pop_back(); ret.push_back(u); for(auto v: graph[u]) { indeg[v]--; if (indeg[v] == 0) todo.push_back(v); } } if (int(ret.size()) != n) return {}; return ret; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; VVI to(n); rep(_, k) { int r, c; cin >> r >> c; to[r - 1].push_back(c - 1); } auto ts = topological_sort(to); if (ts.size() == 0) { cout << -1 << endl; return 0; } VI dp(n); for(int i: ts) for(int j: to[i]) chmax(dp[j], dp[i] + 1); int ans = *max_element(all(dp)) + 1; cout << ans << endl; }