#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, nn;
  cin >> n;
  nn = n;
  vector<int> f, l;
  while (nn--) {
    long long x, y, oldy, mx = -1, mn = 1e10;
    bool isrs = 1;
    cin >> x;
    for (int i = 0; i < x; i++) {
      cin >> y;
      if (i != 0)
        if (oldy < y) {
          isrs = 0;
        }
      mn = min(mn, y);
      mx = max(mx, y);
      oldy = y;
    }
    if (isrs) {
      f.push_back(mx);
      l.push_back(mn);
    }
  }
  long long rans = 0;
  sort(f.begin(), f.end());
  for (int i = 0; i < l.size(); i++) {
    int ind = upper_bound(f.begin(), f.end(), l[i]) - f.begin();
    rans += ind;
  }
  cout << n * n - rans << endl;
  return 0;
}
