#include using namespace std; using LL=long long; using ULL=unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N; vector> S; vector> V; vector I; const int Z=2000; vector appeared; int main(){ cin>>N; S.resize(N); appeared.assign(Z,0); rep(i,N){ int d; cin>>d; S[i].resize(d); rep(j,d){ cin>>S[i][j]; S[i][j]--; appeared[S[i][j]]=1; } } V.resize(Z); I.assign(Z,0); rep(i,N){ if(S[i].front()!=S[i].back()){ V[S[i].front()].push_back(S[i].back()); I[S[i].back()]++; } } queue Q; rep(i,Z) if(I[i]==0) Q.push(i); vector ans; while(Q.size()){ int p=Q.front(); Q.pop(); ans.push_back(p); for(int e:V[p]) if(--I[e]==0) Q.push(e); } if(ans.size()!=Z) cout<<0< ans2; for(int a:ans) if(appeared[a]) ans2.push_back(a); cout<