#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef vector vi; typedef pair pii; #define MP make_pair #define PB push_back #define inf 1000000007 #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define all(x) (x).begin(),(x).end() template void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } template inline bool chmax(T &a, T b){ if(a inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } class tsort { public: int V; vector > G; vector deg,res; tsort(int node_size) : V(node_size), G(V), deg(V, 0){} void add_edge(int from,int to){ G[from].push_back(to); deg[to]++; } bool solve() { queue que; for(int i = 0; i < V; i++){ if(deg[i] == 0){ que.push(i); } } while(!que.empty()){ int p = que.front(); que.pop(); res.push_back(p); for(int v : G[p]){ if(--deg[v] == 0){ que.push(v); } } } return (*max_element(deg.begin(),deg.end()) == 0); } }; vector > a; int main(){ int n; cin >> n; a.resize(n); vectorres; tsort ts(2000); set st; rep(i,n){ int d; cin >> d; a[i].resize(d); rep(z,d){ cin >> a[i][z]; a[i][z]--; if(d!=1){ if(z==d-1){ if(a[i][z]!=a[i][0]){ ts.add_edge(a[i][0],a[i][z]); } st.insert(a[i][z]); } } } } if(!ts.solve()){ cout << 0 << endl; }else{ cout << 2000 << endl; auto X = ts.res; int id = -1; rep(i,2000){ if(st.count(X[i])){ id = i; } } rep(i,2000){ if(id==i)continue; cout << X[i]+1 << " "; } cout << X[id]+1; cout << endl; } return 0; }