#include <bits/stdc++.h>
using namespace std;
void swap(int &a, int &b) {
  int temp = a;
  a = b;
  b = temp;
}
void solve() {
  int n;
  cin >> n;
  vector<pair<int, int>> v;
  for (int i = 1; i <= n; ++i) {
    int n1;
    cin >> n1;
    vector<int> temp(n1);
    bool flag = true;
    for (int j = 0; j < n1; ++j) {
      cin >> temp[j];
      if (j > 0) {
        if (temp[j] > temp[j - 1]) flag = false;
      }
    }
    if (flag) v.push_back(make_pair(temp.back(), temp[0]));
  }
  long long total = 1LL * n * n;
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); ++i)
    total -=
        v.end() - lower_bound(v.begin(), v.end(), make_pair(v[i].second, -1));
  cout << total << "\n";
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ;
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
