#include <bits/stdc++.h>
using namespace std;
long long bit[1123456];
void upd(long long a, long long x) {
  if (a == 0) return;
  for (long long i = a; i < 1123456; i += (i & -i)) bit[i] += x;
}
long long get(long long a) {
  long long resp = 0;
  for (long long i = a; i > 0; i -= (i & -i)) resp += bit[i];
  return resp;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > rel;
  long long tot = n;
  long long resp = 0;
  for (long long i = 0; i < n; i++) {
    long long m;
    cin >> m;
    vector<long long> at;
    long long last = INT_MAX;
    long long mini = INT_MAX;
    long long maxi = 0;
    bool tem = 0;
    for (long long i = 0; i < m; i++) {
      long long x;
      cin >> x;
      if (x > last) tem = 1;
      maxi = max(maxi, x);
      mini = min(mini, x);
      last = x;
    }
    if (!tem) rel.push_back(make_pair(mini + 2, maxi + 2));
  }
  for (auto x : rel) upd(x.second, 1);
  for (auto x : rel) {
    resp += get(x.first);
  }
  cout << n * n - resp << endl;
  return 0;
}
