#include typedef long long ll; typedef long double ld; using namespace std; #define rep(i, a, n) for (int i = (a); i < (n); i++) #define per(i, a, n) for (int i = (n) - 1; i >= (a); i--) const int INF = 1000000000; int main(void) { ios::sync_with_stdio(false); int n, k; while(cin>>n>>k) { vector> a(n*2); rep(i,0,n) { int m; cin >> m; rep(j,0,m) { int x; cin >> x; a[i].push_back(x-1); } } rep(i,0,n) { a[i+n] = a[i]; } vector cur(k,0); int to = 0; int tot = 0; int missing = k; int res = INF; rep(fr,0,n*2) { while(missing > 0 && to < n*2) { for(auto &x : a[to]) { if(cur[x] == 0) missing--; cur[x]++; } tot+=a[to].size(); to++; } if (missing == 0) { res = min(res, tot); } for (auto &x : a[fr]) { cur[x]--; if(cur[x] == 0) missing++; } tot -= a[fr].size(); } if (res == INF) { cout << -1 << endl; } else cout << res << endl; } return 0; }