#include <bits/stdc++.h>
const int MAXN = 1e9 + 7;
const int INF = 1e9 + 7;
using namespace std;
bool comp1(pair<signed long long, signed long long> a,
           pair<signed long long, signed long long> b) {
  if (a.second == b.second) return a.first < b.first;
  return (a.second < b.second);
}
const signed long long N = 10e6 + 10;
signed long long cnt1[N];
signed long long cnt2[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  signed long long n;
  cin >> n;
  signed long long ans = 0;
  signed long long was = 0;
  for (signed long long i = 0; i < n; i++) {
    signed long long k;
    cin >> k;
    signed long long mx = 0;
    signed long long mn = 10e6 + 2;
    bool is = false;
    for (signed long long j = 0; j < k; j++) {
      signed long long y;
      cin >> y;
      if (y > mn) is = true;
      if (y > mx) mx = y;
      if (y < mn) mn = y;
    }
    if (is) {
      was++;
    } else {
      cnt1[mn + 1]++;
      cnt2[mx + 1]++;
    }
  }
  for (signed long long i = N - 2; i > -1; i--) {
    cnt2[i] += cnt2[i + 1];
  }
  ans += 2 * was * (n - was) + was * was;
  for (signed long long i = 0; i < N; i++) {
    ans += cnt1[i] * cnt2[i + 1];
  }
  cout << ans << "\n";
  return 0;
}
