#include <bits/stdc++.h>
using namespace std;
long double pi = 3.14159265358979323846;
vector<long long> al[500005];
long long vis[500005], color[500005], I[101][101];
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
bool isPrime(int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long binpow(long long n, long long po) {
  long long res = 1;
  while (po > 0) {
    if (po % 2) {
      res *= n;
      po--;
    } else {
      n *= n;
      po /= 2;
    }
  }
  return res;
}
long long swap(long long a, long long b) {
  a = a + b;
  b = a - b;
  a = a - b;
}
long long fact(long long n) {
  if ((n == 0) || (n == 1))
    return 1;
  else
    return n * fact(n - 1);
}
long long C(long long n, long long r) {
  if (r > n - r) r = n - r;
  long long ans = 1;
  long long i;
  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
    ans = ans % 1000000007;
  }
  return ans;
}
void mmul(long long A[][101], long long B[][101], long long dim) {
  long long res[dim][dim];
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) {
      res[i][j] = 0;
      for (long long k = 0; k < dim; k++)
        res[i][j] = (res[i][j] + A[i][k] * B[k][j]) % 1000000007;
    }
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) A[i][j] = res[i][j];
}
void mpow(long long A[][101], long long dim, long long po) {
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) {
      if (i == j)
        I[i][j] = 1;
      else
        I[i][j] = 0;
    }
  while (po > 0) {
    if (po % 2 == 1) {
      mmul(I, A, dim);
      po--;
    } else {
      mmul(A, A, dim);
      po /= 2;
    }
  }
  for (long long i = 0; i < dim; i++)
    for (long long j = 0; j < dim; j++) A[i][j] = I[i][j];
}
int main() {
  long long n, k = 0, ans = 0;
  cin >> n;
  vector<long long> v[n], f, l;
  long long a[n], size[n];
  for (long long i = 0; i < n; i++) a[i] = 0;
  for (long long i = 0; i < n; i++) {
    cin >> size[i];
    long long x;
    cin >> x;
    v[i].push_back(x);
    for (long long j = 1; j < size[i]; j++) {
      cin >> x;
      v[i].push_back(x);
      if (v[i][j] > v[i][j - 1]) a[i] = 1;
    }
  }
  for (long long i = 0; i < n; i++) {
    if (a[i] == 0) {
      f.push_back(v[i][0]);
      l.push_back(v[i][size[i] - 1]);
    }
  }
  ans = n * n;
  sort(f.begin(), f.end());
  sort(l.begin(), l.end());
  long long li = 0, y = 0, z = 0;
  while (li < l.size() && y < l.size()) {
    while (l[li] < f[y]) {
      ans -= y;
      li++;
      if (li == l.size()) goto label;
    }
    if (l[li] >= f[y]) y++;
  }
label:
  ans -= (l.size() - li) * y;
  cout << ans;
}
