#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n;
  cin >> n;
  long long temp = n;
  long long res = 0;
  vector<long long> maxvec;
  vector<long long> minvec;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    long long a[x + 3];
    bool flag = false;
    long long mini = INT_MAX;
    long long maxi = INT_MIN;
    for (long long j = 0; j < x; j++) {
      cin >> a[j];
      mini = min(mini, a[j]);
      maxi = max(maxi, a[j]);
    }
    for (long long j = 1; j < x; j++) {
      if (a[j - 1] < a[j]) {
        flag = true;
        res += (2 * temp - 1);
        temp--;
        break;
      }
    }
    if (!flag) {
      maxvec.push_back(maxi);
      minvec.push_back(mini);
    }
  }
  sort(maxvec.begin(), maxvec.end());
  sort(minvec.begin(), minvec.end());
  for (long long i = 0; i < maxvec.size(); i++) {
    res +=
        (lower_bound(minvec.begin(), minvec.end(), maxvec[i]) - minvec.begin());
  }
  cout << res << '\n';
  return 0;
}
