#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct event {
  int car, v, end_t;
  bool operator<(const event& e) const {
    return end_t != e.end_t ? end_t < e.end_t : car < e.car;
  }
  bool operator==(const event& e) const {
    return car == e.car and v == e.v and end_t == e.end_t;
  }
};
int n;
int x[100] = {};
queue<int> v[100];
template <typename T>
long long count_inversions(vector<T>& a, vector<T>& b, int s, int e) {
  if (e - s == 1) {
    return 0;
  } else {
    int m = (s + e) / 2;
    long long ans = count_inversions(a, b, s, m) + count_inversions(a, b, m, e);
    int l_head = s, r_head = m;
    for (int i = s; i < e; i++) {
      if (r_head >= e or (l_head < m and a[l_head] <= a[r_head])) {
        b[i] = a[l_head++];
      } else {
        b[i] = a[r_head++];
        ans += m - l_head;
      }
    }
    for (int i = s; i < e; i++) {
      a[i] = b[i];
    }
    return ans;
  }
}
template <typename T>
long long count_inversions(vector<T>& a) {
  vector<T> b(a.size());
  return count_inversions(a, b, 0, a.size());
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int s;
  cin >> n >> s;
  set<event> events;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    int time = 0;
    while (k--) {
      int vj, tj;
      cin >> vj >> tj;
      v[i].push(vj);
      time += tj;
      events.insert({i, vj, time});
    }
    v[i].push(1);
  }
  vector<int> previous_order;
  int time = 0;
  ll ans = 0;
  for (const event& e : events) {
    int dt = e.end_t - time;
    for (int i = 0; i < n; i++) {
      x[i] += v[i].front() * dt;
    }
    vector<int> index_of(n);
    if (not previous_order.empty()) {
      for (int i = 0; i < n; i++) {
        index_of[previous_order[i]] = i;
      }
    }
    vector<int> curr_order(n);
    iota(curr_order.begin(), curr_order.end(), 0);
    sort(curr_order.begin(), curr_order.end(), [&](int i, int j) {
      if (x[i] != x[j]) {
        return x[i] < x[j];
      } else if (previous_order.empty()) {
        return v[i].front() < v[j].front();
      } else {
        return index_of[i] < index_of[j];
      }
    });
    if (not previous_order.empty()) {
      vector<int> relative_order(n);
      for (int i = 0; i < n; i++) {
        relative_order[i] = index_of[curr_order[i]];
      }
      ans += count_inversions(relative_order);
    }
    previous_order = curr_order;
    v[e.car].pop();
    time = e.end_t;
  }
  cout << ans << '\n';
  return 0;
}
