#include<iostream>
#include<cstdio>
#include<cassert>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<vector> 
#include<cstring>
#include<string>
#include<map>
#include<set>

#define mp make_pair
#define scn second
#define frs first
#define pb push_back

#define NAME "director"

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef pair<ll, int> pi;
const int P = 239017;
const ll INF = 10000000000000000ll;

int n, k;
int sum[10000];
vector<int> w[1000];

int main(){
    freopen(NAME ".in", "r", stdin); 
    freopen(NAME ".out", "w", stdout); 
	scanf("%d%d", &n, &k);
	int smx = 0;
	vector<int> mxs;
	for (int i = 0; i < n; ++i) {
		int t;
		scanf("%d", &t);
		int y;	
		int mx = 0;
		sum[i] = 0;
		for (int j = 0; j < t; ++j)			
			scanf("%d", &y), w[i].pb(y), mx = max(mx, y), sum[i] += y;
		mxs.pb(mx);		
		sum[i] -= mx;
		smx += mx;
   	}
   	sort(sum, sum + n);
   	sort(mxs.begin(), mxs.end());
   	int ans = 1;
   	for (int i = 0; i < n; ++i)
   		if (sum[i] > mxs[i])
   			ans = 0;	
   	if (ans == 0)
   		printf("-1");
   	else
   		printf("%d", smx);

    return 0;
}




