#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long long MAXN = 1e5 + 5;
void solve() {
  long long n;
  cin >> n;
  long long mn[n], mx[n], valid = 0;
  vector<long long> mnn, mxx;
  for (long long i = 0; i < n; i++) {
    long long l;
    cin >> l;
    mn[i] = 1e9, mx[i] = -1e9;
    bool ok = 0;
    for (long long j = 0; j < l; j++) {
      long long x;
      cin >> x;
      if (x > mn[i]) ok = 1;
      mn[i] = min(mn[i], x);
      mx[i] = max(mx[i], x);
    }
    if (ok)
      valid++;
    else
      mnn.push_back(mn[i]), mxx.push_back(mx[i]);
  }
  long long cnt = valid * valid + 2 * valid * (n - valid);
  sort(mxx.begin(), mxx.end());
  for (auto i : mnn)
    cnt += mxx.end() - lower_bound(mxx.begin(), mxx.end(), i + 1);
  cout << cnt << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tt = 1;
  while (tt--) {
    solve();
  }
  return 0;
}
