#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
int n, k;
vector<vector<int> > v;
vector<lint> d;
vector<lint> solve(int c){
lint tot = 0;
for(int i=0; i<n; i++) tot += v[i][0];
auto cmp = [&](const vector<pi> &p, const vector<pi> &q){
lint r1 = 0, r2 = 0;
for(auto &i : p){
r1 += v[i.first][i.second] - v[i.first][0];
}
for(auto &i : q){
r2 += v[i.first][i.second] - v[i.first][0];
}
return r1 > r2;
};
priority_queue<vector<pi>, vector<vector<pi>>, decltype(cmp)> pq(cmp);
set<vector<pi>> vis;
vector<lint> dap;
vector<pi> w;
for(int j=0; j<c; j++) w.push_back(pi(j, 1));
pq.push(w);
while(!pq.empty() && dap.size() < k){
auto cur = pq.top();
pq.pop();
if(vis.find(cur) != vis.end()) continue;
vis.insert(cur);
lint ret = tot;
for(auto &i : cur){
ret += v[i.first][i.second] - v[i.first][0];
}
if(d.size() + dap.size() >= k && ret >= d.back()) continue;
dap.push_back(ret);
for(int i=0; i<c; i++){
auto nxt = cur;
if(nxt[i].second + 1 < v[nxt[i].first].size()){
nxt[i].second++;
pq.push(nxt);
nxt[i].second--;
}
if(nxt[i].second == 1 && i < c-1 && nxt[i].first + 1 < nxt[i+1].first){
nxt[i].first++;
pq.push(nxt);
nxt[i].first--;
}
if(nxt[i].second == 1 && i == c-1 && nxt[i].first + 1 < n){
nxt[i].first++;
pq.push(nxt);
nxt[i].first--;
}
}
}
return dap;
}
int main(){
freopen("roboherd.in", "r", stdin);
freopen("roboherd.out", "w", stdout);
cin >> n >> k;
lint ret = 0;
d.push_back(0);
for(int i=0; i<n; i++){
int x;
scanf("%d",&x);
vector<int> w(x);
for(int j=0; j<x; j++){
scanf("%d",&w[j]);
}
if(x == 1){
ret += 1ll * w[0] * k;
}
else{
sort(w.begin(), w.end());
v.push_back(w);
d[0] += w[0];
}
}
sort(v.begin(), v.end(), [&](const vector<int> &a, const vector<int> &b){
return a[1] - a[0] < b[1] - b[0];
});
n = v.size();
for(int i=1; i<=16 && i<=n; i++){
vector<lint> w = solve(i);
for(auto &j : w) d.push_back(j);
sort(d.begin(), d.end());
while(d.size() > k) d.pop_back();
}
for(int i=0; i<k; i++) ret += d[i];
cout << ret;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxpbnQ7CnR5cGVkZWYgcGFpcjxpbnQsIGludD4gcGk7CgppbnQgbiwgazsKdmVjdG9yPHZlY3RvcjxpbnQ+ID4gdjsKdmVjdG9yPGxpbnQ+IGQ7Cgp2ZWN0b3I8bGludD4gc29sdmUoaW50IGMpewoJbGludCB0b3QgPSAwOwoJZm9yKGludCBpPTA7IGk8bjsgaSsrKSB0b3QgKz0gdltpXVswXTsKCglhdXRvIGNtcCA9IFsmXShjb25zdCB2ZWN0b3I8cGk+ICZwLCBjb25zdCB2ZWN0b3I8cGk+ICZxKXsKCQlsaW50IHIxID0gMCwgcjIgPSAwOwoJCWZvcihhdXRvICZpIDogcCl7CgkJCXIxICs9IHZbaS5maXJzdF1baS5zZWNvbmRdIC0gdltpLmZpcnN0XVswXTsKCQl9CgkJZm9yKGF1dG8gJmkgOiBxKXsKCQkJcjIgKz0gdltpLmZpcnN0XVtpLnNlY29uZF0gLSB2W2kuZmlyc3RdWzBdOwoJCX0KCQlyZXR1cm4gcjEgPiByMjsKCX07Cglwcmlvcml0eV9xdWV1ZTx2ZWN0b3I8cGk+LCB2ZWN0b3I8dmVjdG9yPHBpPj4sIGRlY2x0eXBlKGNtcCk+IHBxKGNtcCk7CglzZXQ8dmVjdG9yPHBpPj4gdmlzOwoJdmVjdG9yPGxpbnQ+IGRhcDsKCgl2ZWN0b3I8cGk+IHc7Cglmb3IoaW50IGo9MDsgajxjOyBqKyspIHcucHVzaF9iYWNrKHBpKGosIDEpKTsKCXBxLnB1c2godyk7CgkKCXdoaWxlKCFwcS5lbXB0eSgpICYmIGRhcC5zaXplKCkgP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