#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j, ans = 0, cnt = 1;
  vector<long long int> asc, maxi, mini;
  cin >> n;
  for (i = 0; i < n; i++) {
    long long int l, x, mn = INT_MAX, mx = -1, flag = 0;
    cin >> l;
    for (j = 0; j < l; j++) {
      cin >> x;
      if (x > mx) mx = x;
      if (x < mn) mn = x;
      if (x > mn) flag = 1;
    }
    if (flag == 1) {
      asc.push_back(1);
      ans = ans + n + n - cnt;
      maxi.push_back(-1);
      mini.push_back(-1);
      cnt += 2;
    } else {
      asc.push_back(0);
      maxi.push_back(mx);
      mini.push_back(mn);
    }
  }
  sort(maxi.begin(), maxi.end());
  vector<long long int>::iterator itr;
  long long int k;
  for (i = 0; i < n; i++) {
    if (asc[i]) continue;
    itr = upper_bound(maxi.begin(), maxi.end(), mini[i]);
    if (itr != maxi.end()) {
      k = itr - maxi.begin();
      ans += n - k;
    }
  }
  cout << ans << endl;
  return 0;
}
