#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
struct node {
  long long _max, _min;
} num[N];
long long n, m, str[N], last, tot, res;
inline bool cmp(node a, node b) { return a._max > b._max; }
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &m);
    bool f = 0;
    last = -1;
    for (long long i = 1; i <= m; i++) {
      scanf("%lld", &str[i]);
      if (f) continue;
      if (last == -1) last = str[i];
      if (last < str[i]) f = 1;
      last = str[i];
    }
    if (f) continue;
    num[++tot]._max = str[1];
    num[tot]._min = str[m];
  }
  sort(num + 1, num + tot + 1, cmp);
  for (long long i = 1; i <= tot; i++) {
    long long l = 1, r = tot;
    while (l <= r) {
      long long mid = l + r >> 1;
      if (num[i]._min < num[mid]._max)
        l = mid + 1;
      else
        r = mid - 1;
    }
    res += tot - l + 1;
  }
  printf("%lld\n", n * n - res);
  return 0;
}
