#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<long long> v[n];
  for (int i = 0; i < n; i++) {
    int t, a;
    cin >> t;
    while (t--) {
      cin >> a;
      v[i].push_back(a);
    }
  }
  long long ans = 0;
  vector<long long> high(n, 0), low(n, 100000000);
  int nn = n;
  for (int i = 0; i < n; i++) {
    int t, a = 0, f = v[i][0], ff = 1;
    high[i] = 0;
    low[i] = 100000000;
    t = v[i].size();
    for (int j = 1; j < t; j++) {
      if (v[i][j] > f) {
        ans += ((2 * nn) - 1);
        nn--;
        high[i] = -1;
        low[i] = 10000000;
        ff = 0;
        break;
      }
      f = v[i][j];
    }
    if (ff)
      for (int j = 0; j < t; j++) {
        high[i] = max(high[i], v[i][j]);
        low[i] = min(low[i], v[i][j]);
      }
  }
  sort(low.begin(), low.end());
  for (int i = 0; i < n; i++) {
    long long h = high[i];
    std::vector<long long>::iterator l1, l2, l3;
    ans += (lower_bound(low.begin(), low.end(), h) - low.begin());
  }
  cout << ans;
  return 0;
}
