#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
void solve() {
  long long int n, count = 0, as = 0;
  cin >> n;
  vector<long long int> min, max;
  for (long long int j = 0; j < n; j++) {
    long long int l;
    cin >> l;
    long long int mi = INT64_MAX, ma = 0, flag = 0;
    for (long long int i = 0; i < l; i++) {
      long long int t;
      cin >> t;
      if (t < mi) mi = t;
      if (t > ma) ma = t;
      if (t > mi) flag = 1;
    }
    if (flag)
      as++;
    else {
      min.emplace_back(mi);
      max.emplace_back(ma);
    }
  }
  sort(max.begin(), max.end());
  for (long long int i = 0; i < min.size(); i++) {
    long long int it = max.end() - upper_bound(max.begin(), max.end(), min[i]);
    count += it;
  }
  count += 2 * as * (n - as) + as * as;
  cout << count << '\n';
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
