#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 101001, inf = 1e9;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long int n;
  cin >> n;
  vector<int> vec[n];
  vector<int> max1;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    int l;
    cin >> l;
    int max2 = -100;
    for (int j = 0; j < l; j++) {
      int a;
      cin >> a;
      vec[i].push_back(a);
      max2 = max(max2, a);
    }
    bool have = 0;
    for (int j = 0; j < vec[i].size() - 1; j++) {
      if (vec[i][j] < vec[i][j + 1]) {
        have = 1;
      }
    }
    if (!have) {
      max1.push_back(max2);
    } else {
      cnt++;
    }
  }
  sort(max1.begin(), max1.end());
  long long int ans = 0;
  for (int i = 0; i < n; i++) {
    bool have = 0;
    int min1 = vec[i][0];
    for (int j = 0; j < vec[i].size() - 1; j++) {
      if (vec[i][j] < vec[i][j + 1]) {
        have = 1;
      }
      min1 = min(min1, vec[i][j + 1]);
    }
    if (have) {
      ans += n;
      continue;
    }
    auto it = upper_bound(max1.begin(), max1.end(), min1);
    int dif = max1.end() - it;
    ans += dif;
    ans += cnt;
  }
  cout << ans;
}
