#include using namespace std; #define FOR(i,a,b) for(auto i=a;i; using vvi=vector; using pii=pair; int main(){ ios::sync_with_stdio(false);cout.tie(0);cin.tie(0); while (1) { ll n, k; cin >> n >> k; if (!cin) break; vector has(k+1, 0); vvi st(2*n); for (ll i = 0; i < n; ++ i){ ll l; cin >> l; st[i].resize(l); st[i + n].resize(l); for (ll j = 0; j < l; ++ j) { cin >> st[i][j]; st[i + n][j] = st[i][j]; } } ll s = 0, e = 0; ll cs = 0; ll hc = 0; ll res = numeric_limits::max(); while (1) { if (e == st.size() && hc < k) break; if (hc >= k && s < e) { for (ll i = 0; i < st[s].size(); ++ i) { //if (has[st[s][i]]) { has[st[s][i]] --; if (h; } has[st[s][i]] --; if (!has[st[s][i]]) hc --; } cs -= st[s].size(); s ++; } else if (e < st.size()) { for (ll i = 0; i < st[e].size(); ++ i) { //if (!has[st[e][i]]) { has[st[e][i]] = true; hc ++; } if (!has[st[e][i]]) hc ++; has[st[e][i]] ++; } cs += st[e].size(); e ++; } if (hc >= k) res = min(res, cs); } cout << (res==numeric_limits::max()?-1:res) << endl; } return 0; }