#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(std::ios::floatfield);
  std::cout.precision(50);
  long long a, b, c, d, x, i, j, n, t, m, k, y;
  double p, s, q, r, w;
  string str = "", str1 = "";
  long long a1, a2, b1, b2;
  cin >> n;
  vector<long long> vmi, vma;
  long long good = 0;
  for (i = 0; i < n; i = i + 1) {
    cin >> m;
    b = 1e18;
    c = 0;
    d = 0;
    vector<long long> v1;
    for (j = 0; j < m; j = j + 1) {
      cin >> a;
      b = min(b, a);
      if (b < a) {
        d = 1;
      }
      v1.push_back(a);
      c = max(c, a);
    }
    if (d) {
      good++;
    } else {
      vmi.push_back(b);
      vma.push_back(c);
    }
  }
  sort((vmi).begin(), (vmi).end());
  sort((vma).begin(), (vma).end());
  long long count1 = good * n + (n - good) * good;
  for (i = 0; i < vmi.size(); i = i + 1) {
    a = vmi[i];
    vector<long long>::iterator it = upper_bound((vma).begin(), (vma).end(), a);
    count1 += (vma.end() - it);
  }
  cout << count1;
}
