#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1000000 + 5;
int a[N], b[N];
int main() {
  int n, k1 = 0;
  scanf("%d", &n);
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    int t, x, minn = INF, maxn = -1, flag = 0;
    scanf("%d", &t);
    int z = 0;
    for (int j = 0; j < t; j++) {
      scanf("%d", &x);
      if (x > minn && flag == 0) {
        flag = 1;
        sum += n;
      }
      if (x < minn) minn = x;
      if (x > maxn) maxn = x;
    }
    if (flag == 0) {
      a[minn]++;
      b[maxn]++;
      k1++;
    }
  }
  sum += k1 * (n - k1);
  long long p = 0;
  for (int i = 0; i <= 1000000; i++) {
    if (b[i] != 0) {
      sum = sum + b[i] * p;
    }
    p += a[i];
  }
  printf("%lld\n", sum);
  return 0;
}
