#include <bits/stdc++.h>
using namespace std;
const int SZ = 1e5 + 5;
int n, m, len[SZ], maxi[SZ], mini[SZ], flag[SZ];
long long ans;
vector<int> v;
int main() {
  int i, j, l;
  scanf("%d", &n);
  m = n;
  for (i = 1; i <= n; i++) {
    scanf("%d", &len[i]);
    maxi[i] = -1;
    mini[i] = 1 << 30;
    for (j = 1; j <= len[i]; j++) {
      scanf("%d", &l);
      if (mini[i] < l) flag[i] = 1;
      maxi[i] = max(maxi[i], l);
      mini[i] = min(mini[i], l);
    }
  }
  for (i = 1; i <= n; i++) {
    if (flag[i]) {
      ans += 2 * (m - 1) + 1;
      m--;
      continue;
    }
    v.emplace_back(mini[i]);
  }
  sort((v).begin(), (v).end());
  for (i = 1; i <= n; i++) {
    if (flag[i]) continue;
    ans += lower_bound((v).begin(), (v).end(), maxi[i]) - v.begin();
  }
  printf("%lld\n", ans);
  return 0;
}
