#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b % a, a);
}
long long pow(long long a, long long b) {
  if (b == 0) return 1;
  if (b == 1) return a;
  long long half = pow(a, b / 2);
  long long ans = (half * half);
  if (b % 2) {
    ans = (ans * a);
  }
  return ans;
}
long long my_lb(vector<long long> &V, long long val) {
  long long l = 0, r = V.size() - 1;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (V[mid] >= val) {
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  return r;
}
int main() {
  long long n, x, y, z;
  cin >> n;
  vector<long long> V1, V2;
  long long a_count = 0;
  for (int i = 0; i < n; i++) {
    cin >> x;
    cin >> y;
    long long curr_min = y;
    long long curr_max = y;
    bool is_ascent = false;
    for (int i = 1; i < x; i++) {
      cin >> z;
      if (z > curr_min) {
        is_ascent = true;
      }
      curr_min = min(curr_min, z);
      curr_max = max(curr_max, z);
    }
    if (!is_ascent) {
      V1.push_back(curr_min);
      V2.push_back(curr_max);
    } else {
      a_count++;
    }
  }
  sort(V1.begin(), V1.end());
  sort(V2.begin(), V2.end());
  long long ans = 0;
  long long na_len = V1.size();
  for (int i = 0; i < na_len; i++) {
    long long idx = my_lb(V1, V2[i]);
    ans += (idx + 1);
  }
  ans += ((a_count * (n - a_count) * 2) + (a_count * a_count));
  cout << ans << endl;
  return 0;
}
