#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
long long int arr1[N], arr2[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    int m = 1e6 + 5, M = -1;
    bool b = 0;
    vector<int> v;
    for (int j = 0; j < l; j++) {
      int x;
      cin >> x;
      v.push_back(x);
      M = max(M, x);
      m = min(m, x);
    }
    int z = v[0];
    for (int i = 1; i < v.size(); i++) {
      if (v[i] > z) {
        b = 1;
        break;
      }
      z = min(z, v[i]);
    }
    if (b) {
      ans += n;
      arr2[N - 2]++;
      continue;
    } else {
      arr1[m]++;
      arr2[M]++;
    }
  }
  for (int i = N - 2; i >= 0; i--) {
    arr2[i] += arr2[i + 1];
  }
  for (int i = 0; i < N; i++) {
    ans += arr1[i] * arr2[i + 1];
  }
  cout << ans << '\n';
  return 0;
}
