#include <bits/stdc++.h>
using namespace std;
bool dp[100005];
bool ascent(vector<int> v) {
  for (int i = 1; i < v.size(); i++) {
    if (v[i - 1] < v[i]) return true;
  }
  return false;
}
int main() {
  long long int n, in, si, ans = 0;
  cin >> n;
  long long int low[n], big[n];
  for (int i = 0; i < n; i++) low[i] = 1e9, big[i] = 0;
  for (int i = 0; i < n; i++) {
    cin >> si;
    vector<int> v;
    for (int j = 0; j < si; j++) {
      cin >> in;
      v.emplace_back(in);
      low[i] = min(low[i], in);
      big[i] = max(big[i], in);
    }
    if (ascent(v)) {
      ans += 2 * (n - 1) + 1;
      low[i] = 1e9, big[i] = 0;
      i--;
      n--;
    }
  }
  sort(big, big + n);
  for (int i = 0; i < n; i++) {
    int l = 0, r = n - 1;
    while (r - l > 1) {
      int mid = (l + r) / 2;
      if (low[i] < big[mid])
        r = mid;
      else
        l = mid;
    }
    if (low[i] < big[l])
      ans += n - l;
    else if (low[i] < big[r])
      ans += n - r;
  }
  cout << ans << '\n';
  return 0;
}
