#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int MAXN = 101;
const int MAXM = 10080;
int N, M;
vector<int> T[MAXN];

int main() {
  while(cin >> N >> M && (N|M)) {
    for(int i = 0; i < MAXN; ++i) T[i].clear();
    for(int i = 0; i < N; ++i) {
      int a;
      while(cin >> a && a) {
	T[i].push_back(a);
      }
    }
    queue<int> que;
    int ju_den = -1;
    int p[MAXN];
    int t[MAXN];
    fill(p,p+MAXN, 0);
    for(int j = 0; j < N; ++j) t[j] = T[j][0];
    int ans = 0;
    for(int i = 0; i < M; ++i) {
      for(int j = 0; j < N; ++j) {
	if(p[j]%2 == 0) {
	  --t[j];
	  if(t[j] == 0) {
	    p[j] = (p[j]+1)%T[j].size();
	    t[j] = T[j][p[j]];
	    que.push(j);
	  }
	} else {
	  if(ju_den == j) {
	    --t[j];
	    if(t[j] == 0) {
	      p[j] = (p[j]+1)%T[j].size();
	      t[j] = T[j][p[j]];
	      ju_den = -1;
	    }
	  } else {
	    ++ans;
	  }
	}
      }
      if(que.size() && ju_den == -1) {
	ju_den = que.front();
	que.pop();
      }
    }
    cout << ans << endl;
  }
  return 0;
}