#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
const int M = 1e6;
const long long INF = (long long)(1e18);
const int inf = 1e9;
const long long MOD = 1000000007LL;
void solve() {}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<int> start, last;
  long long ans = 0LL;
  long long ones = 0LL;
  for (long long i = 0LL; i < n; i++) {
    int l;
    cin >> l;
    int mini = inf;
    vector<int> a;
    bool ok = false;
    for (int j = 0; j < l; j++) {
      int x;
      cin >> x;
      mini = min(mini, x);
      a.push_back(x);
      if (x > mini) {
        ok = true;
      }
    }
    if (!ok) {
      if (a[0] == a.back()) ones++;
      start.push_back(a[0]);
      last.push_back(a.back());
    }
  }
  sort(start.begin(), start.end());
  sort(last.begin(), last.end());
  for (int i = 0; i < last.size(); i++) {
    int cur = last[i];
    auto it = upper_bound(start.begin(), start.end(), cur);
    it--;
    long long tmp = (long long)(it - start.begin() + 1);
    ans += tmp;
  }
  cout << n * n - ans;
  return 0;
}
