// // Created by yamunaku on 2021/02/19. // #include //#include using namespace std; //using namespace atcoder; #define rep(i, n) for(int i = 0; i < (n); i++) #define repl(i, l, r) for(int i = (l); i < (r); i++) #define per(i, n) for(int i = ((n)-1); i >= 0; i--) #define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--) #define all(x) (x).begin(),(x).end() #define MOD9 998244353 #define MOD1 1000000007 #define IINF 1000000000 #define LINF 1000000000000000000 #define SP <<" "<< #define CYES cout<<"Yes"<; using mti = vector>; using vl = vector; using mtl = vector>; using pi = pair; using pl = pair; template using heap = priority_queue, function>; int main() { //CFS; int n; cin >> n; mti s(n); int m = 2000; vi use(m); mti e(m); vi deg(m); mti id(m); rep(i, n) { int k; cin >> k; s[i] = vi(k); rep(j, k) cin >> s[i][j], s[i][j]--; use[s[i].back()] = true; id[s[i].back()].push_back(i); } rep(i, n) { if (s[i].front() != s[i].back()) { if (use[s[i].front()]) { e[s[i].front()].push_back(s[i].back()); deg[s[i].back()]++; } } } vi ord; queue q; rep(i, m) { if (deg[i] == 0 && use[i]) q.push(i); } while (!q.empty()) { int now = q.front(); q.pop(); ord.push_back(now); for (auto &nx : e[now]) { deg[nx]--; if (deg[nx] == 0) q.push(nx); } } rep(i, m) { if (deg[i] > 0) { cout << 0 << endl; return 0; } } vi ans; for (int x : ord) { ans.push_back(x); for (auto &t : id[x]) { int idx = s[t].size() - 1; per(i, ans.size()) { if (idx >= 0) if (ans[i] == s[t][idx]) { idx--; } } vi tmp; rep(i, idx + 1) { tmp.push_back(s[t][i]); } for (auto &y : ans) tmp.push_back(y); ans = tmp; } } cout << ans.size() << endl; for (auto &x : ans) cout << x + 1 << " "; cout << endl; return 0; }