#include <bits/stdc++.h>
using namespace std;
bool no_ascent(int s[], int l) {
  int prev = s[0];
  for (int i = 0; i < l; ++i) {
    if (s[i] > prev) return false;
    prev = s[i];
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  while (T-- > 0) {
    unsigned long long n;
    cin >> n;
    unsigned long long ans = 0, c = 0;
    vector<int> first, last;
    for (int i = 0; i < n; ++i) {
      int l;
      cin >> l;
      int s[l];
      for (int j = 0; j < l; ++j) {
        cin >> s[j];
      }
      if (no_ascent(s, l)) {
        first.push_back(s[0]);
        last.push_back(s[l - 1]);
      }
    }
    sort((last).begin(), (last).end());
    int new_size = first.size();
    for (int fst : first) {
      int loc = lower_bound((last).begin(), (last).end(), fst) - last.begin();
      c += new_size - loc;
    }
    ans = n * n - c;
    cout << ans;
    cout << '\n';
  }
  return 0;
}
