#include <bits/stdc++.h>
using namespace std;
int a[100][30010];
int n, s, j, k, v, t, maxn, ans, x;
int main() {
  scanf("%d%d", &n, &s);
  for (int i = 0; i < n; i++) {
    scanf("%d", &k);
    x = 0;
    while (k--) {
      scanf("%d%d", &v, &t);
      while (t--) a[i][++x] = a[i][x - 1] + v;
    }
    if (x > maxn) maxn = x;
  }
  for (int i = 2; i <= maxn; i++)
    for (int j = 0; j < n - 1; j++) {
      if (a[j][i] == 0) continue;
      for (k = j + 1; k < n; k++) {
        if (a[k][i] == 0) continue;
        if (a[j][i] == a[k][i]) {
          if (a[j][i - 1] < a[k][i - 1] && a[j][i + 1] > a[k][i + 1]) ans++;
          if (a[j][i - 1] > a[k][i - 1] && a[j][i + 1] < a[k][i + 1]) ans++;
        } else {
          if (a[j][i - 1] < a[k][i - 1] && a[j][i] > a[k][i]) ans++;
          if (a[j][i - 1] > a[k][i - 1] && a[j][i] < a[k][i]) ans++;
        }
      }
    }
  printf("%d\n", ans);
  return 0;
}
