#include <bits/stdc++.h>
using namespace std;
long long mx[100100], mn[100100];
int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long l;
    cin >> l;
    bool flg = 0;
    mx[i] = -1;
    mn[i] = 1000000007;
    while (l--) {
      long long x;
      cin >> x;
      if (x > mn[i]) flg = 1;
      mx[i] = max(mx[i], x);
      mn[i] = min(mn[i], x);
    }
    if (flg) {
      mx[i] = 1000000007;
      mn[i] = -1;
    }
  }
  sort(mx + 1, mx + n + 1);
  sort(mn + 1, mn + n + 1);
  long long sp = 0, ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long b = mx[i];
    for (long long j = sp + 1; j <= n; j++) {
      if (b <= mn[j]) break;
      sp++;
    }
    ans += sp;
  }
  cout << ans << "\n";
  return 0;
}
