#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, a[200009], f = 0, c = 0, ma[200009], mi[200009], ans = 0;
  string s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long m, fl = 0, a[200009];
    cin >> m;
    for (int j = 0; j < m; j++) cin >> a[j];
    for (int j = 1; j < m; j++) {
      if (a[j] > a[j - 1]) fl = 1;
    }
    if (fl == 0) {
      mi[c] = a[m - 1];
      ma[c] = a[0];
      c++;
    }
  }
  sort(mi, mi + c);
  sort(ma, ma + c);
  long long st = 0;
  for (int i = 0; i < c; i++) {
    if (mi[i] < ma[st]) {
      ans += c - st;
    } else {
      while (st < c && mi[i] >= ma[st]) st++;
      ans += c - st;
    }
  }
  ans += (n - c) + (n * (n - 1)) - (c * (c - 1));
  cout << ans;
  return 0;
}
