#include <cstdio>
#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
#include <memory.h>

using namespace std;

const int maxn = 111;
const int inf = 1e9 + 7;

int n, k, m;
int s[maxn], a[maxn][maxn];
int sum[maxn], p[maxn];
int mt[maxn], fl[maxn];
char vis[16];

bool dfs(int v) {
 	if (vis[v]) return 0;
	vis[v] = 1;
	for (int i = 0; i < n; i++) {
	 	if (a[v][p[v]] >= sum[i] && (mt[i] == -1 || dfs(mt[i]))) {
	 	 	mt[i] = v;
			return 1;
		}
	}
	return 0;
}

int main() {
 	freopen("riot.in", "r", stdin);
	freopen("riot.out", "w", stdout);

	cin >> n >> k;
	for (int i = 0; i < n; i++) {
	 	cin >> s[i];
		for (int j = 0; j < s[i]; j++) {
		 	cin >> a[i][j];
			sum[i] += a[i][j];
			m++;
		}
		sort(a[i], a[i] + s[i]);
	}

	srand(666);
	int ans = inf;
	int it = -1;
	while (clock() / (double)CLOCKS_PER_SEC < 1.9) {
		it++;
		int cost = 0;
	 	for (int i = 0; i < n; i++) {
	 	 	for (int j = s[i] - 1; j >= 0; j--) {
	 	 	 	if (it == 0 || j == 0 || rand() % 2 == 0) {
	 	 	 	 	p[i] = j;
					break;
				}
			}
			sum[i] -= a[i][p[i]];
			cost += a[i][p[i]];
		}
		for (int i = 0; i < n; i++) {
			mt[i] = -1;
			fl[i] = 0;
		}
		for (int i = 0; i < n; i++) {
		 	for (int j = 0; j < n; j++) {
		 	 	if (a[i][p[i]] >= sum[j] && mt[j] == -1) {
		 	 	 	mt[j] = i;
					fl[i] = 1;
					break;
				}
			}
		}

		bool ok = 1;
		for (int i = 0; ok && i < n; i++) if (!fl[i]) {
		 	memset(vis, 0, sizeof(vis));
			ok &= dfs(i);
		}
		if (ok) {
			ans = min(ans, cost);
		}
		for (int i = 0; i < n; i++) sum[i] += a[i][p[i]];
	}                                                    
	if (ans == inf) ans = -1;
	cout << ans << endl;

	return 0;
}