#include #define REP(i, n) for(int i = 0 ; i< (int)n; ++i) using namespace std; bool testcase(){ int n; if(!(cin >> n))return false; int k; cin >> k; std::vector counts(k); vector> stands(n); for(int i = 0; i < n; i++) { int l; cin >> l; for(int j = 0; j < l; j++) { int s; cin >> s; s--; stands[i].push_back(s); } } int nempty = k; int ntotal = 0; int start = 0; int end = 0; int best = 1000000000; for(auto& z : stands[start]) { if(counts[z] == 0) nempty--; counts[z] += 1; ntotal += 1; } while(true) { while(nempty > 0) { end = (end + 1) % n; if(end == start) { if(best == 1000000000) cout << -1 << endl; else cout << best << endl; return true; } for(auto& z : stands[end]) { if(counts[z] == 0) nempty--; counts[z] += 1; ntotal += 1; } } //cerr << start << " " << end << " " << ntotal << endl; if(ntotal < best) best = ntotal; for(auto& z : stands[start]) { if(counts[z] == 1) nempty++; counts[z] -= 1; ntotal -= 1; } if(start == end) { end = (end + 1) % n; for(auto& z : stands[end]) { if(counts[z] == 0) nempty--; counts[z] += 1; ntotal += 1; } } start = (start + 1) % n; if(start == 0) break; } if(best == 1000000000) cout << -1 << endl; else cout << best << endl; return true; } int main(){ while(testcase()); return 0; }