#include <bits/stdc++.h>
using namespace std;
long long n, l, c, a[1000100], m[1000100], M[1000100], s[1000100], e[1000100];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    bool asc = false;
    cin >> l;
    m[i] = 1e8;
    for (int j = 0; j < l; j++) {
      cin >> a[j];
      m[i] = min(m[i], a[j]);
      M[i] = max(M[i], a[j]);
      if (a[j] > m[i]) asc = true;
    }
    if (asc) {
      c++;
    } else {
      s[m[i]]++;
      e[M[i]]++;
    }
  }
  long long ans = n * n - (n - c) * (n - c);
  long long f = 0;
  for (int i = 0; i < 1000100; i++) {
    ans += e[i] * f;
    f += s[i];
  }
  cout << ans << endl;
}
