#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, val, count = 0, s = 0;
  cin >> n;
  vector<vector<long long int> > v(n);
  vector<long long int> a(n), m1, m2;
  for (long long int i = 0; i < n; i++) {
    long long int m, flag = 0, min_ele = INT_MAX;
    cin >> m;
    for (long long int j = 0; j < m; j++) {
      cin >> val;
      v[i].push_back(val);
      if (val > min_ele)
        flag = 1;
      else
        min_ele = val;
    }
    if (flag == 1)
      count += n;
    else {
      m1.push_back(*max_element(v[i].begin(), v[i].end()));
      m2.push_back(*min_element(v[i].begin(), v[i].end()));
    }
  }
  sort(m1.begin(), m1.end());
  for (long long int i = 0; i < m2.size(); i++) {
    long long int u = upper_bound(m1.begin(), m1.end(), m2[i]) - m1.begin();
    count += n - u;
  }
  cout << count;
  return 0;
}
