#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f;
struct sequence {
  int max;
  int min;
  bool flag;
} e[100005];
int cntm[1000005];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int pre, t, flag, l, r, nn, temp;
  long long n;
  cin >> n;
  memset(cntm, 0, sizeof(cntm));
  for (int i = 0; i < n; i++) {
    pre = inf;
    cin >> nn;
    e[i].max = -inf;
    e[i].min = inf;
    e[i].flag = false;
    for (int j = 0; j < nn; j++) {
      cin >> temp;
      e[i].max = max(e[i].max, temp);
      e[i].min = min(e[i].min, temp);
      if (temp > e[i].min) e[i].flag = true;
    }
    if (!e[i].flag) cntm[e[i].max]++;
  }
  long long ans = n * n;
  for (int i = 1; i <= 1000000; i++) cntm[i] += cntm[i - 1];
  for (int i = 0; i < n; i++) {
    if (!e[i].flag) ans -= cntm[e[i].min];
  }
  cout << ans;
  return 0;
}
