#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, i, j, ans = 0;
  cin >> n;
  vector<long long> v[n];
  vector<long long> a(n, 0), mi(n, 0), ma(n, 0);
  for (i = 0; i < n; i++) {
    long long s;
    cin >> s;
    long long p1 = INT_MAX, p2 = INT_MIN;
    for (j = 0; j < s; j++) {
      long long temp;
      cin >> temp;
      v[i].push_back(temp);
      p1 = min(p1, temp);
      p2 = max(p2, temp);
    }
    mi[i] = p1;
    ma[i] = p2;
    long long mini = v[i][0];
    for (j = 1; j < s; j++) {
      if (v[i][j] > mini) {
        a[i] = 1;
        break;
      }
      mini = min(mini, v[i][j]);
    }
  }
  vector<long long> vis(n, 0);
  for (i = 0; i < n; i++) {
    if (a[i] == 1)
      vis[i] = -1;
    else
      vis[i] = ma[i];
  }
  ma.clear();
  for (i = 0; i < n; i++) {
    if (vis[i] != -1) ma.push_back(vis[i]);
  }
  sort(ma.begin(), ma.end());
  reverse(ma.begin(), ma.end());
  long long k = *(max_element(ma.begin(), ma.end()));
  vector<long long> dp(k + 1, 0);
  long long pt = 0;
  for (i = k; i >= 0; i--) {
    while (ma[pt] > i and pt < ma.size()) {
      dp[i]++;
      pt++;
    }
    if (i != k) dp[i] += dp[i + 1];
  }
  long long cnt = 0;
  for (i = 0; i < n; i++) {
    if (a[i] == 1) {
      ans += ((2 * n) - 1);
      ans -= cnt;
      cnt += 2;
    } else {
      ans += dp[mi[i]];
    }
  }
  cout << ans;
  return 0;
}
