#include #include #include #include using namespace std; struct State; int n; int end_time; int cost; State* charging; deque cq; struct State { vector pat; bool wait; bool charge; int cur; int cnt; void Init() { pat.clear(); wait = false; charge = false; cur = 0; cnt = 0; } void Run() { if(wait) { ++cost; //cout << "-"; return; } //if(charge) //cout << "."; // else //cout << "*"; if(++cnt >= pat[cur]) { if(charge) { charging = 0; } else { cq.push_back(this); wait = true; } if(++cur >= (int)pat.size()) cur = 0; cnt = 0; charge = !charge; } } }; State st[100]; void Run() { if(charging == 0 && !cq.empty()) { charging = cq.front(); cq.pop_front(); charging->wait = false; } for(int i = 0; i < n; ++i) st[i].Run(); //cout << endl; } int main() { while(cin >> n >> end_time && n) { for(int i = 0; i < n; ++i) { st[i].Init(); int t; while(cin >> t && t) st[i].pat.push_back(t); } cost = 0; charging = 0; cq.clear(); for(int i = 0; i < end_time; ++i) { Run(); } cout << cost << endl; } }