#include using namespace std; int main(){ int n, ans = 0; cin >> n; vector> g(n); vector m(n), d(n); for(int i = 0; i < n; ++i){ cin >> m[i]; int f; cin >> f; g[i].resize(f); for(auto &x : g[i]) cin >> x, --x, ++d[x]; } deque> q; for(int i = 0; i < n; ++i) if(d[i] == 0) q.emplace_back(-m[i],i); sort(q.rbegin(),q.rend()); for(int b = n-1; b >= 0; --b){ ans = max(ans, -q.front().first + b); int u = q.front().second; q.pop_front(); for(auto v : g[u]) if(--d[v] == 0){ auto it=lower_bound(q.rbegin(),q.rend(),make_pair(-m[v],-(1<<29))); q.insert(it.base(),make_pair(-m[v],v)); } } cout << ans << endl; }