#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int n, x;
bool flag[MAXN];
int len[MAXN], minn[MAXN], maxx[MAXN];
vector<int> v, sm;
int main() {
  scanf("%d", &n);
  long long ans = 0;
  memset(flag, 0, sizeof(flag));
  for (int i = 1; i <= n; i++) {
    scanf("%d", &len[i]);
    maxx[i] = -1, minn[i] = 1e6 + 1;
    v.clear();
    for (int j = 1; j <= len[i]; j++) {
      scanf("%d", &x);
      v.push_back(x);
      minn[i] = min(minn[i], x);
      maxx[i] = max(maxx[i], x);
    }
    int now = v[0];
    for (int j = 1; j < len[i]; j++) {
      if (now < v[j]) {
        flag[i] = 1;
        break;
      } else
        now = min(now, v[j]);
    }
    if (flag[i] == 1) sm.push_back(minn[i]);
  }
  sort(minn + 1, minn + n + 1);
  sort(sm.begin(), sm.end());
  int num = sm.size();
  for (int i = 1; i <= n; i++) {
    int pos = lower_bound(minn + 1, minn + n + 1, maxx[i]) - minn;
    if (!flag[i]) {
      int tmp = lower_bound(sm.begin(), sm.end(), maxx[i]) - sm.begin();
      ans += 1LL * (pos + num - tmp - 1);
    } else
      ans += 1LL * n;
  }
  printf("%I64d\n", ans);
  return 0;
}
