#include <bits/stdc++.h>
const int inf = int(1e9);
const int mod = inf + 7;
const double PI = acos(-1.0);
using namespace std;
const int N = 1e5 + 155;
template <typename T>
inline void read(T& x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
long long bp(long long a, long long n) {
  long long r = 1;
  while (n) {
    if (n & 1) r = (r * a) % mod;
    a = (a * a) % mod;
    n >>= 1;
  }
  return r;
}
void solve() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> mx, mn;
  int tmp = 0;
  for (int i = 0; i < n; ++i) {
    vector<int> t;
    int l;
    cin >> l;
    for (int i = 0; i < l; ++i) {
      int x;
      cin >> x;
      t.push_back(x);
    }
    for (int i = 0; i < l - 1; ++i) {
      if (t[i] < t[i + 1]) {
        ans += 2 * n;
        t.clear();
        tmp++;
        break;
      }
    }
    if (!t.empty()) {
      sort((t).begin(), (t).end());
      mn.push_back(t.front());
      mx.push_back(t.back());
    }
  }
  ans -= (tmp * 1ll * tmp);
  sort((mn).begin(), (mn).end());
  sort((mx).begin(), (mx).end());
  int j = 0;
  n -= tmp;
  for (int i = 0; i < n; ++i) {
    if (mn[i] < mx[j]) {
      ans += (n - j);
    } else {
      for (; j < n; j++) {
        if (mn[i] < mx[j]) {
          break;
        }
      }
      if (j == n) break;
      ans += (n - j);
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
