#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000 * 100 + 5;
long long minn[maxn], maxx[maxn], a[10 * maxn], n, cnt = 0;
long long ans = 0;
long long check(long long x) {
  long long l = 0, r = n + 1;
  while (r - l > 1) {
    long long mid = (r + l) / 2;
    if (a[mid] > x) {
      r = mid;
    } else {
      l = mid;
    }
  }
  return n - l;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  set<int> s, t;
  a[n + 1] = 1e9;
  fill(minn, minn + maxn, (long long)1e9);
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    bool b = true;
    for (int j = 1; j <= k; j++) {
      long long x;
      cin >> x;
      minn[i] = min(minn[i], x);
      maxx[i] = max(maxx[i], x);
      if (minn[i] < x) {
        b = false;
      }
    }
    if (b)
      a[i] = (maxx[i]);
    else {
      cnt++;
      minn[i] = (long long)1e9;
      a[i] = 0;
    }
  }
  int i = 1;
  sort(a + 1, a + n + 1);
  for (i = 1; i <= n; i++) {
    if (minn[i] < (long long)1e9) ans += check(minn[i]);
  }
  cout << ans + cnt * (n) + cnt * (n - cnt);
}
