#include <bits/stdc++.h>
using namespace std;
int main() {
  long long cnt = 0;
  long long n;
  cin >> n;
  vector<long long> minn;
  vector<long long> maxx;
  vector<long long> check;
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    long long mn = INT_MAX;
    long long mx = 0;
    vector<long long> ss;
    int f = 0;
    for (int j = 0; j < e; j++) {
      long long c;
      cin >> c;
      if (c > mx) mx = c;
      if (c < mn) mn = c;
      if (c > mn) f = 1;
      ss.push_back(c);
    }
    check.push_back(f);
    if (f == 1)
      cnt++;
    else {
      minn.push_back(mn);
      maxx.push_back(mx);
    }
  }
  cnt = (2 * cnt * n - cnt * cnt);
  long long ccc = 0;
  sort(minn.begin(), minn.end());
  sort(maxx.begin(), maxx.end());
  for (int i = 0; i < minn.size(); i++) {
    auto upper = upper_bound(maxx.begin(), maxx.end(), minn[i]);
    ccc += minn.size() - (upper - maxx.begin());
  }
  cnt += ccc;
  cout << cnt << endl;
}
