#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long inf = 92233720000000000ll;
const long long nax = 100001;
const long long mxm = 1000002;
long long freq[mxm];
pair<long long, long long> a[nax];
long long n, x, mx, mn, ans, sz;
void solve() {
  ans = 0;
  cin >> n;
  for (long long i = (0); i < (n); i++) {
    long long last = inf;
    cin >> sz;
    bool ok = 0;
    long long mn = inf, mx = -1;
    for (long long j = (0); j < (sz); j++) {
      cin >> x;
      ok |= (x > last);
      mn = min(mn, x);
      mx = max(mx, x);
      last = x;
    }
    a[i] = {mn, ok};
    if (ok)
      freq[mxm - 1]++;
    else
      freq[mx]++;
  }
  for (long long i = (1); i < (mxm); i++) freq[i] += freq[i - 1];
  for (long long i = (0); i < (n); i++) {
    if (a[i].second)
      ans += n;
    else
      ans += freq[mxm - 1] - freq[a[i].first];
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
