#include <bits/stdc++.h>
using namespace std;
int n;
int l;
int a[1000007];
int minn[1000007], maxn[1000007], cnt;
int cmp(int x, int y) { return x > y; }
int main() {
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &l);
    int flag = 0;
    for (int j = 1; j <= l; j++) {
      scanf("%d", &a[j]);
      if (j == 1) continue;
      if (a[j] > a[j - 1]) flag = 1;
    }
    if (flag == 0) {
      minn[++cnt] = a[l];
      maxn[cnt] = a[1];
    }
  }
  sort(minn + 1, minn + 1 + cnt);
  for (int i = 1; i <= cnt; i++) {
    int pos = lower_bound(minn + 1, minn + 1 + cnt, maxn[i]) - minn - 1;
    ans += cnt - pos;
  }
  printf("%lld", 1ll * n * n - ans);
  return 0;
}
