#include <bits/stdc++.h>
using namespace std;
const double pai = acos(-1);
const double eps = 1e-8;
const long long mod = 1e9 + 7;
const int MXN = 1e6 + 5;
vector<int> v[MXN];
int c[MXN];
int suf[MXN];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int le;
    scanf("%d", &le);
    while (le--) {
      int aa;
      scanf("%d", &aa);
      v[i].push_back(aa);
    }
    int mi = INT_MAX;
    for (auto j : v[i]) {
      if (j > mi) c[i] = 1;
      mi = min(mi, j);
    }
    sort(v[i].begin(), v[i].end());
  }
  long long gg = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i]) {
      gg++;
      continue;
    }
    suf[v[i].back()]++;
  }
  for (int i = 1e6 - 1; i >= 0; i--) suf[i] = suf[i] + suf[i + 1];
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i])
      ans = ans + (long long)n;
    else
      ans = ans + (long long)suf[v[i][0] + 1] + gg;
  }
  cout << ans << '\n';
  return 0;
}
