#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int inf = 1e6 + 5;
int a[inf], b[inf];
int main() {
  int n;
  cin >> n;
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    int ma = 0, mi = inf, x, y;
    bool f = 0;
    cin >> x;
    for (int j = 0; j < x; ++j) {
      cin >> y;
      if (y > mi) f = 1;
      if (y > ma) ma = y;
      if (y < mi) mi = y;
    }
    if (!f) {
      a[mi]++;
      b[ma]++;
    }
  }
  long long ans1 = 0;
  for (int i = 1; i < inf; ++i) b[i] += b[i - 1];
  for (int i = 0; i < inf; ++i)
    if (a[i] > 0) ans1 = ans1 + (a[i] * 1.0 * b[i]);
  long long q = n * 1.0 * n;
  q -= ans1;
  cout << q << endl;
  return 0;
}
