#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long maxn = 1e5 + 10;
vector<long long> a[maxn];
long long lo[maxn], hi[maxn];
const long long inf = 1e15;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(9);
  long long n;
  cin >> n;
  long long cnt = 0;
  map<long long, long long> mp;
  vector<long long> keep;
  for (long long i = 0; i < n; ++i) {
    long long len;
    cin >> len;
    bool ok = false;
    long long mn = inf;
    long long mx = -inf;
    for (long long j = 0; j < len; ++j) {
      long long cur;
      cin >> cur;
      a[i].push_back(cur);
      if (cur > mn) ok = true;
      mn = min(mn, cur);
      mx = max(mx, cur);
    }
    lo[i] = mn;
    hi[i] = mx;
    if (ok) {
      ++cnt;
      hi[i] = inf;
    } else {
      keep.push_back(mx);
    }
  }
  sort(keep.begin(), keep.end());
  long long res = 0;
  for (long long i = 0; i < n; ++i) {
    if (hi[i] == inf) {
      res += n;
    } else {
      long long cur = cnt;
      auto it = upper_bound(keep.begin(), keep.end(), lo[i]);
      if (it != keep.end()) {
        long long ind = it - keep.begin();
        long long is = (long long)keep.size() - ind;
        cur += is;
      }
      res += cur;
    }
  }
  cout << res << '\n';
  return 0;
}
