Problem: Bill is a boss of security guards. He has pride in that his men put on wearable computers on their duty. At the same time, it is his headache that capacities of commercially available batteries are far too small to support those computers all day long. His men come back to the office to charge up their batteries and spend idle time until its completion. Bill has only one battery charger in the office because it is very expensive.

Bill suspects that his men spend much idle time waiting in a queue for the charger. If it is the case, Bill had better introduce another charger. Bill knows that his men are honest in some sense and blindly follow any given instructions or rules. Such a simple-minded way of life may lead to longer waiting time, but they cannot change their behavioral pattern.

Each battery has a data sheet attached on it that indicates the best pattern of charging and consuming cycle. The pattern is given as a sequence of pairs of consuming time and charging time. The data sheet says the pattern should be followed cyclically to keep the battery in quality. A guard, trying to follow the suggested cycle strictly, will come back to the office exactly when the consuming time passes out, stay there until the battery has been charged for the exact time period indicated, and then go back to his beat.

The guards are quite punctual. They spend not a second more in the office than the time necessary for charging up their batteries. They will wait in a queue, however, if the charger is occupied by another guard, exactly on first-come-first-served basis. When two or more guards come back to the office at the same instance of time, they line up in the order of their identifi- cation numbers, and, each of them, one by one in the order of that line, judges if he can use the charger and, if not, goes into the queue. They do these actions in an instant.

Your mission is to write a program that simulates those situations like Bill’s and reports how much time is wasted in waiting for the charger.



Input

The input consists of one or more data sets for simulation.

The first line of a data set consists of two positive integers separated by a space character: the number of guards and the simulation duration. The number of guards does not exceed one hundred. The guards have their identification numbers starting from one up to the number of guards. The simulation duration is measured in minutes, and is at most one week, i.e., 10080 (min.).

Patterns for batteries possessed by the guards follow the first line. For each guard, in the order of identification number, appears the pattern indicated on the data sheet attached to his battery. A pattern is a sequence of positive integers, whose length is a multiple of two and does not exceed fifty. The numbers in the sequence show consuming time and charging time alternately. Those times are also given in minutes and are at most one day, i.e., 1440 (min.). A space character or a newline follows each number. A pattern is terminated with an additional zero followed by a newline.

Each data set is terminated with an additional empty line. The input is terminated with an additional line that contains two zeros separated by a space character.

Output

For each data set your program should simulate up to the given duration. Each guard should repeat consuming of his battery (i.e., being on his beat) and charging of his battery according to the given pattern cyclically. At the beginning, all the guards start their cycle simultaneously, that is, they start their beats and, thus, start their first consuming period.

For each data set, your program should produce one line containing the total wait time of the guards in the queue up to the time when the simulation duration runs out. The output should not contain any other characters.

For example, consider a data set:


3 25
3 1 2 1 4 1 0
1 1 0
2 1 3 2 0


The guard 1 tries to repeat 3 min. consuming, 1 min. charging, 2 min. consuming, 1 min. charging, 4 min. consuming, and 1 min. charging, cyclically. Yet he has to wait sometimes to use the charger, when he is on his duty together with the other guards 2 and 3. Thus, the actual behavior of the guards looks like:


0         10        20
|    |    |    |    |    |
guard 1: ***.**.****.***.**-.****.
guard 2: *.*-.*-.*-.*.*.*.*--.*.*-
guard 3: **.***--..**-.***..**.***


where “*” represents a minute spent for consuming, “.” for charging, and “-” for waiting in the queue. At time 3, the guards 1 and 2 came back to the office and the guard 1 started charging while the guard 2 went into the queue. At time 6, all the guards came back to the office and the guard 1 started charging while the others went to the queue. When the charger got available at time 7, the guard 2 started charging, leaving the guard 3 in the queue. All those happened are consequences of rules stated above. And the total time wasted in waiting for the charger becomes 10 minutes.

Example

Input

3 25
3 1 2 1 4 1 0
1 1 0
2 1 3 2 0

4 1000
80 20 80 20 80 20 80 20 0
80
20
0
80 20 90
10 80
20
0
90 10
0

0 0


Output

10
110

Solution (in CPP):

Solution:
#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
#include <assert.h>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

class State {
public:
  int _id;
  int _time;
  int _occupy;
  State(int id,int time,int occupy) : _id(id),_time(time), _occupy(occupy) {}
  bool operator<(const State& s) const {
    if(_time == s._time) return _id < s._id;
    return _time < s._time;
  }
  bool operator>(const State& s) const {
    if(_time == s._time) return _id > s._id;
    return _time > s._time;
  }
};

int main(){
  int num_of_guards;
  int simulation_duration;
  while(~scanf("%d %d",&num_of_guards,&simulation_duration)){
    if(num_of_guards == 0 && simulation_duration == 0) break;

    vector<int> time_table[101];
    for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      int comsuming_duration;
      int charging_duration;
      while(~scanf("%d",&comsuming_duration)){
        if(comsuming_duration == 0) break;
        scanf("%d",&charging_duration);
        time_table[guard_i].push_back(comsuming_duration);
        time_table[guard_i].push_back(charging_duration);
      }
    }
    
    int for_queue[101];
    int next_process[101];
    bool is_inque[101];
    memset(next_process,0,sizeof(next_process));
    memset(for_queue,0,sizeof(for_queue));
    memset(is_inque,false,sizeof(is_inque));

    priority_queue<State,vector<State>,greater<State> > que;
    int res = 0;
    for(int time = 0; time < simulation_duration;time++){
      char action[101];
      memset(action,'-',sizeof(action));
      for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
        if(next_process[guard_i] % 2 == 0){ // to consume
          if(is_inque[guard_i]){
            continue;
          }
          for_queue[guard_i] = time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()] - 1;
          next_process[guard_i]++;
          action[guard_i] = '*';
        }
        else{ //to charge
          if(for_queue[guard_i] > 0){
            action[guard_i] = '*';
            for_queue[guard_i]--;
            continue;
          }

          que.push(State(guard_i,time,time_table[guard_i][next_process[guard_i] % time_table[guard_i].size()]));
          next_process[guard_i]++;
          is_inque[guard_i] = true;
        }
      }
      if(!que.empty()){
        State s = que.top();
        que.pop();
        action[s._id] = '.';
        if(s._occupy - 1 > 0){
          que.push(State(s._id,s._time,s._occupy-1));
        }
        else{
          is_inque[s._id] = false;
        }
      }

      res += count(action,action+num_of_guards,'-');

      // for debug
      // printf("time:%d\n",time);
      // for(int guard_i = 0; guard_i < num_of_guards; guard_i++){
      //         printf("%c\n",action[guard_i]);
      // }
      // printf("\n");
    }

    printf("%d\n",res);
  }
}