#include <bits/stdc++.h>
using namespace std;
static bool compare(pair<long long int, long long int>& p1,
                    pair<long long int, long long int>& p2) {
  return p1.second < p2.second;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  t = 1;
  for (long long int p = 0; p < t; p++) {
    long long int n;
    cin >> n;
    vector<long long int> mini(n, INT_MAX);
    vector<long long int> maxi(n, INT_MIN);
    vector<long long int> ascent(n, 0);
    for (long long int i = 0; i < n; i++) {
      long long int num;
      cin >> num;
      for (long long int j = 0; j < num; j++) {
        long long int nums;
        cin >> nums;
        if (nums > mini[i]) ascent[i] = 1;
        mini[i] = min(mini[i], nums);
        maxi[i] = max(maxi[i], nums);
      }
      if (ascent[i] == 1) {
        mini[i] = -1;
        maxi[i] = INT_MAX;
      }
    }
    sort(maxi.begin(), maxi.end());
    long long int count = 0;
    for (int x : mini) {
      long long int ris = maxi.end() - upper_bound(maxi.begin(), maxi.end(), x);
      count += ris;
    }
    cout << count << endl;
  }
  return 0;
}
