#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int t, n, x, maxn, minn, a[N], maxx[N], cnt1, cnt2, miin[N], flag;
int main() {
  scanf("%d", &t);
  memset(a, 0, sizeof(a));
  for (int i = 1; i <= t; i++) {
    minn = 1000000, maxn = 0, flag = 0;
    scanf("%d", &n);
    for (int j = 1; j <= n; j++) {
      scanf("%d", &x);
      minn = min(minn, x);
      maxn = max(maxn, x);
      if (x > minn) {
        a[i] = 1;
        if (!flag) {
          ++cnt1;
          flag++;
        }
      }
    }
    if (!a[i]) {
      maxx[++cnt2] = maxn;
      miin[i] = minn;
    }
  }
  sort(maxx + 1, maxx + 1 + cnt2);
  long long ans = 0;
  for (int i = 1; i <= t; i++) {
    if (a[i])
      ans += t;
    else {
      ans += (cnt1 + cnt2 -
              (upper_bound(maxx + 1, maxx + 1 + cnt2, miin[i]) - maxx) + 1);
    }
  }
  printf("%lld\n", ans);
}
