#include <bits/stdc++.h>
using namespace std;
long long maxm[5000005], vis[5000005], ans;
vector<long long> minn;
int main() {
  long long n, acent = 0, k = 0;
  scanf("%lld", &n);
  for (long long i = 1; i < n + 1; i++) {
    long long l, mn = 1000005;
    scanf("%lld", &l);
    long long a[l + 5];
    for (long long j = 0; j < l; j++) scanf("%lld", &a[j]);
    for (long long j = 0; j < l; j++) {
      mn = min(mn, a[j]);
      if (mn < a[j]) {
        acent++;
        vis[i] = 1;
        break;
      }
    }
    sort(a, a + l);
    if (!vis[i]) {
      maxm[k] = a[0];
      minn.push_back(a[l - 1]);
      k++;
    }
  }
  sort(maxm, maxm + k);
  sort(minn.begin(), minn.end());
  for (long long i = 0; i < k; i++) {
    auto it = upper_bound(minn.begin(), minn.end(), maxm[i]);
    ans += minn.end() - it;
  }
  ans += (acent * acent) + (2 * (n - acent) * acent);
  cout << ans;
}
