#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 100;
int T;
int n, m;
int a[maxn], sum[maxn], val[maxn];
bool vis[maxn];
bool check() {
  for (int i = 2; i <= m; i++) {
    if (a[i] > a[i - 1]) return false;
  }
  return true;
}
long long ans = 0;
int M[maxn], X[maxn], mx, mi = 1e9;
int main() {
  scanf("%d", &n);
  int cnt = n;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &m);
    mx = 0, mi = 1e9;
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[j]);
      mi = min(mi, a[j]);
      mx = max(mx, a[j]);
    }
    if (check())
      vis[i] = true, cnt--;
    else
      continue;
    sum[i] = mi;
    val[i] = mx;
    M[mi]++;
    X[mx]++;
  }
  for (int i = 1; i <= 1e6; i++) M[i] += M[i - 1], X[i] += X[i - 1];
  for (int i = 1; i <= n; i++) {
    if (!vis[i])
      ans += 2 * n;
    else {
      ans += M[val[i] - 1];
      ans += X[maxn - 100] - X[sum[i]];
      ans += (cnt)*2;
    }
  }
  cout << ans / 2 << endl;
  return 0;
}
