#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 1;
int cnt[N];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  using ll = long long;
  vector<int> tail;
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    vector<int> a(m);
    bool no = true;
    for (int j = 0; j < m; ++j) {
      cin >> a[j];
      if (j && a[j - 1] < a[j]) {
        no = false;
      }
    }
    if (no) {
      ++cnt[a[0]];
      tail.push_back(a[m - 1]);
    }
  }
  ll ans = 0;
  for (int i = 1; i < N; ++i) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = 0; i < tail.size(); ++i) {
    ans += cnt[tail[i]];
  }
  ans = 1LL * n * n - ans;
  cout << ans << '\n';
  return 0;
}
