#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long i, j, k, m, p, h = 0, n, kk = 0, x, mm;
  cin >> n;
  long long b[n];
  vector<pair<long long, long long> > v;
  vector<long long> a[n];
  vector<long long> vv, v1;
  for (i = 0; i < n; i++) {
    cin >> m;
    p = 0;
    cin >> x;
    a[i].push_back(x);
    for (j = 1; j < m; j++) {
      cin >> x;
      a[i].push_back(x);
      if (a[i][j] > a[i][j - 1]) {
        p = 1;
      }
    }
    sort(a[i].begin(), a[i].end());
    if (a[i][0] != a[i][m - 1]) h++;
    if (p == 0) {
      b[i] = 0;
      vv.push_back(i);
      v1.push_back(a[i][m - 1]);
    } else {
      b[i] = 1;
      kk++;
    }
    v.push_back(make_pair(a[i][0], a[i][m - 1]));
  }
  sort(v1.begin(), v1.end());
  for (i = 0; i < n; i++) {
    if (b[i] == 1) {
      h += (n - 1);
    } else {
      h += kk;
      mm = v1.size() -
           (upper_bound(v1.begin(), v1.end(), v[i].first) - v1.begin());
      h += mm;
      if (v[i].first != v[i].second) h--;
    }
  }
  cout << h << endl;
  return 0;
}
