#include <bits/stdc++.h>
using namespace std;
bool comp(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first > b.first) return true;
  if (a.first == b.first) return a.second > b.second;
  return false;
}
int main() {
  int n;
  scanf("%d", &n);
  vector<pair<int, int> > v;
  for (int i = 0; i < n; i++) {
    int k;
    scanf("%d", &k);
    int a[100001];
    int ok = 1;
    for (int i = 0; i < k; i++) {
      scanf("%d", &a[i]);
      if (i && a[i] > a[i - 1]) ok = 0;
    }
    if (ok) v.push_back(make_pair(a[0], a[k - 1]));
  }
  sort(v.begin(), v.end(), comp);
  vector<int> lo, hi;
  for (auto i : v) {
    hi.push_back(i.first);
    lo.push_back(i.second);
  }
  long long ans = 0;
  for (int i = 0; i < v.size(); i++) {
    int m = upper_bound(hi.rbegin(), hi.rend(), lo[i]) - hi.rbegin();
    ans += m;
  }
  printf("%lld", ((long long)n * (long long)n) - ans);
}
