#include <bits/stdc++.h>
const int MAXN = 1e6;
int n, mn[MAXN + 5], Sum[MAXN + 5];
bool f[MAXN + 5];
inline void Work() {
  scanf("%d", &n);
  long long Ans = 0;
  for (register int i = 1, l, s, mx; i <= n; ++i) {
    scanf("%d", &l), mn[i] = 0x3f3f3f3f, mx = 0;
    f[i] = false;
    for (register int j = 1; j <= l; ++j) {
      scanf("%d", &s);
      if (s > mn[i]) f[i] = true;
      if (s < mn[i]) mn[i] = s;
      if (s > mx) mx = s;
    }
    if (!f[i]) ++Sum[mx];
  }
  for (register int i = 1; i <= 1e6; ++i) Sum[i] += Sum[i - 1];
  for (register int i = 1; i <= n; ++i) {
    Ans += n - !f[i] * Sum[mn[i]];
  }
  printf("%lld\n", Ans);
}
int main() {
  Work();
  return 0;
}
