Today s kilometer long auto race takes place in Berland. The track is represented by a straight line as long as s kilometers. There are n cars taking part in the race, all of them start simultaneously at the very beginning of the track. For every car is known its behavior — the system of segments on each of which the speed of the car is constant. The j-th segment of the i-th car is pair (vi, j, ti, j), where vi, j is the car's speed on the whole segment in kilometers per hour and ti, j is for how many hours the car had been driving at that speed. The segments are given in the order in which they are "being driven on" by the cars.

Your task is to find out how many times during the race some car managed to have a lead over another car. A lead is considered a situation when one car appears in front of another car. It is known, that all the leads happen instantly, i. e. there are no such time segment of positive length, during which some two cars drive "together". At one moment of time on one and the same point several leads may appear. In this case all of them should be taken individually. Meetings of cars at the start and finish are not considered to be counted as leads.

Input

The first line contains two integers n and s (2 ≤ n ≤ 100, 1 ≤ s ≤ 106) — the number of cars and the length of the track in kilometers. Then follow n lines — the description of the system of segments for each car. Every description starts with integer k (1 ≤ k ≤ 100) — the number of segments in the system. Then k space-separated pairs of integers are written. Each pair is the speed and time of the segment. These integers are positive and don't exceed 1000. It is guaranteed, that the sum of lengths of all segments (in kilometers) for each car equals to s; and all the leads happen instantly.

Output

Print the single number — the number of times some car managed to take the lead over another car during the race.

Examples

Input

2 33
2 5 1 2 14
1 3 11


Output

1


Input

2 33
2 1 3 10 3
1 11 3


Output

0


Input

5 33
2 1 3 3 10
1 11 3
2 5 3 3 6
2 3 1 10 3
2 6 3 3 5


Output

2
#include <bits/stdc++.h>
using namespace std;
template <class T>
void splitstr(const string &s, vector<T> &out) {
  istringstream in(s);
  out.clear();
  copy(istream_iterator<T>(in), istream_iterator<T>(), back_inserter(out));
}
template <class T>
void initArray(T *arr, int size, T value) {
  for (int i = 0; i < size; i++) {
    arr[i] = value;
  }
}
template <class T>
T readValue(string s) {
  T result;
  istringstream sin(s);
  sin >> result;
  return result;
}
template <class T>
inline T gcd(T a, T b) {
  return (!a) ? b : gcd(b % a, a);
}
template <class T>
inline T mod(T a, T p) {
  a %= p;
  return (a < 0) ? a + p : a;
}
template <class T>
inline int numbits(T n) {
  return (!n) ? 0 : 1 + numbits(n & (n - 1));
}
template <class T>
inline T inverse(T a, T m) {
  a = mod<T>(a, m);
  return (a == 1) ? 1 : mod((1 - m * inverse(m % a, a)) / a, m);
}
template <class T>
inline bool isPrime(T a) {
  T lim = (T)sqrt((double)a);
  for (T i = 2; i < lim; i++)
    if (a % i == 0) return false;
  return true;
}
template <class T>
inline T power(T a, T p, T mod) {
  if (!p) return 1;
  T temp = power(a, p >> 1, mod);
  temp = (temp * temp) % mod;
  if (p & 1) temp = (temp * a) % mod;
  return temp;
}
void get_primes(int start, int end, vector<int> &vi) {
  int *p = new int[end + 1];
  initArray<int>(p, end + 1, 0);
  p[1] = 1;
  for (int i = 2; i <= end; i++) {
    if (!p[i]) {
      if (i >= start) vi.push_back(i);
      for (int j = 2 * i; j <= end; j += i) p[j] = 1;
    }
  }
}
bool dfs(int current, int final, int total, int *visited, int **edges,
         bool flow) {
  if (current == final) return true;
  if (visited[current]) return false;
  visited[current] = true;
  for (int i = 0; i < total; i++)
    if (edges[current][i] && dfs(i, final, total, visited, edges, flow)) {
      if (flow) {
        edges[current][i]--;
        edges[i][current]++;
      }
      return true;
    }
  return false;
}
int flow(int in, int out, int total, int **edges) {
  int result = 0;
  int *visited = new int[total];
  while (initArray<int>(visited, total, 0),
         dfs(in, out, total, visited, edges, true))
    result++;
  return result;
}
void create_set(int x, int *P, int *rank) {
  P[x] = x;
  rank[x] = 0;
}
int find_set(int x, int *P) {
  if (x != P[x]) P[x] = find_set(P[x], P);
  return P[x];
}
bool merge_sets(int x, int y, int *P, int *rank) {
  int Px = find_set(x, P);
  int Py = find_set(y, P);
  if (Px == Py) return false;
  if (rank[Px] > rank[Py])
    P[Py] = Px;
  else
    P[Px] = Py;
  if (rank[Px] == rank[Py]) rank[Py]++;
  return true;
}
int read_cum_freq(int index, int *tree) {
  int sum = 0;
  while (index) {
    sum += tree[index];
    index -= (index & -index);
  }
  return sum;
}
void upd_freq(int index, int mxIndex, int value, int *tree) {
  while (index <= mxIndex) {
    tree[index] += value;
    index += (index & -index);
  }
}
int read_freq(int index, int *tree) {
  return read_cum_freq(index, tree) - read_cum_freq(index - 1, tree);
}
void build_failure_function(const string &str, int *temp_arr) {
  temp_arr[0] = temp_arr[1] = 0;
  for (int i = 2; i <= static_cast<int>(str.size()); i++) {
    temp_arr[i] = -1;
    for (int ind = temp_arr[i - 1]; temp_arr[i] == -1; ind = temp_arr[ind]) {
      if (str[ind] == str[i - 1]) {
        temp_arr[i] = ind + 1;
      } else if (ind == 0) {
        temp_arr[i] = 0;
      }
    }
  }
}
void KMP(const string &s1, const string &s2, int *res) {
  int *temp_arr = new int[s2.size() + 1];
  build_failure_function(s2, temp_arr);
  int i = 0;
  int ind = 0;
  while (i < static_cast<int>(s1.size())) {
    if (s1[i] == s2[ind]) {
      res[i] = ++ind;
      if (res[i] == s2.size()) {
        ind = temp_arr[ind - 1];
      }
      i++;
    } else if (ind == 0) {
      res[i++] = 0;
    } else {
      ind = temp_arr[ind];
    }
  }
  delete[] temp_arr;
}
void setup(int value) {
  freopen("input.txt", "r", stdin);
  if (value) {
    freopen("output.txt", "w", stdout);
  }
}
int v[101][102];
int t[101][102];
int k[101];
int main() {
  memset(v, 0, sizeof(v));
  memset(t, 0, sizeof(t));
  int n, s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
    for (int j = 0; j < k[i]; j++) {
      cin >> v[i][j] >> t[i][j + 1];
      t[i][j + 1] += t[i][j];
    }
  }
  int res = 0;
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++) {
      int first = (v[i][0] > v[j][0]) ? 0 : 1;
      int cur_t = 0;
      int cur_ti = 0;
      int cur_tj = 0;
      int cur_xi = 0;
      int cur_xj = 0;
      while (cur_ti != k[i] && cur_tj != k[j]) {
        int next_t = (std::min)(t[i][cur_ti + 1], t[j][cur_tj + 1]);
        int next_xi = cur_xi + (next_t - cur_t) * v[i][cur_ti];
        int next_xj = cur_xj + (next_t - cur_t) * v[j][cur_tj];
        if (next_xi > next_xj && first == 1) {
          first = 0;
          res++;
        }
        if (next_xi < next_xj && first == 0) {
          first = 1;
          res++;
        }
        cur_t = next_t;
        cur_xi = next_xi;
        cur_xj = next_xj;
        if (t[i][cur_ti + 1] < t[j][cur_tj + 1])
          cur_ti++;
        else
          cur_tj++;
      }
    }
  std::cout << res << std::endl;
  return 0;
}
