#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char c;
  for (c = getchar(); !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
}
const int N = 1e5 + 50, P = 1e9 + 7;
int mul(int a, int b) { return 1ll * a * b % P; }
int add(int a, int b) {
  a += b;
  return a >= P ? a - P : a;
}
int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + P : a;
}
int gcd(int a, int b) { return !b ? a : gcd(b, a % b); }
int n, mn[N], mx[N], id1[N], id2[N], s[N], ok[N];
long long ans;
int main(int argc, char *argv[]) {
  read(n);
  for (int i = 1; i <= (n); ++i) {
    int l;
    read(l);
    mn[i] = 1e9;
    for (int k = 1; k <= (l); ++k) {
      int t;
      read(t);
      if (t > mn[i]) ok[i] = 1;
      (mx[i] < t ? mx[i] = t : mx[i]);
      (mn[i] > t ? mn[i] = t : mn[i]);
    }
  }
  for (int i = 1; i <= (n); ++i) id1[i] = id2[i] = i;
  sort(id1 + 1, id1 + n + 1, [](int x, int y) { return mn[x] < mn[y]; });
  for (int i = n; i >= (1); --i) s[i] = s[i + 1] + ok[id1[i]];
  sort(id2 + 1, id2 + n + 1, [](int x, int y) { return mx[x] < mx[y]; });
  int p = 1;
  for (int i = 1; i <= (n); ++i) {
    while (p <= n && mn[id1[p]] < mx[id2[i]]) ++p;
    ans += ok[id2[i]] ? n : p - 1 + s[p];
  }
  printf("%lld\n", ans);
  return 0;
}
