#include <bits/stdc++.h>
using namespace std;
int n, m, Cnt, big[100005], sma[100005], bn, sn;
unsigned long long ans;
struct Node {
  int Max, Min, flag;
} a[100005];
int main() {
  cin >> n;
  for (int i = 1; i < 1 + n; i++) {
    cin >> m;
    int tem, ma = -0x3f3f3f3f, mi = 0x3f3f3f3f;
    while (m--) {
      cin >> tem;
      if (tem > mi) a[i].flag = 1;
      ma = max(ma, tem), mi = min(mi, tem);
    }
    a[i].Max = ma, a[i].Min = mi;
  }
  for (int i = 1; i < n + 1; i++) {
    if (a[i].flag)
      Cnt++;
    else {
      big[bn] = a[i].Max;
      sma[sn] = a[i].Min;
      bn++, sn++;
    }
  }
  ans = (unsigned long long)n * (unsigned long long)n -
        (unsigned long long)(n - Cnt) * (unsigned long long)(n - Cnt);
  sort(sma, sma + sn);
  for (int i = 0; i < sn; i++) {
    int tem = lower_bound(sma, sma + sn, big[i]) - sma;
    ans += tem;
  }
  cout << ans << endl;
  return 0;
}
