#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,cnt;
int rd0[N],rd1[N];
vector<int> pic[N];
queue<int> q;
void topo_sort();
int main(){
int v;
cin>>n;
for(int i=1;i<=n;i++){
while(cin>>v&&v!=0){
pic[v].push_back(i);
rd0[i]++;
}
}
for(int i=1;i<=n;i++){
//cout<<rd0[i]<<endl;
for(int j=1;j<=n;j++){
rd1[j]=rd0[j];
}
q.push(i);
topo_sort();
}
return 0;
}
void topo_sort(){
cnt=0;
while(!q.empty()){
int x=q.front();
q.pop();
int len=pic[x].size();
for(int i=0;i<len;i++){
int Next=pic[x][i];
rd1[Next]--;
if(rd1[Next]==0){
q.push(Next);
cnt++;
}
}
}
cout<<cnt<<endl;
return;
}