#include <bits/stdc++.h>
using namespace std;
void fn() {}
inline int Rand() { return (rand() << 16) | rand(); }
const double eps = 1e-9, Pi = acos(-1);
const int N = 3e2 + 5, second = 1e7;
const int MOD = 1e9 + 7, inf = 21e8;
const long long INF = 9e18;
string s[N], t[N];
int d[int(2e6)];
int main() {
  int n;
  long long ans = 0, num = 0;
  cin >> n;
  vector<pair<int, int> > v;
  map<int, int> w;
  for (int i = 1; i <= n; i++) {
    int q;
    cin >> q;
    int mn = 1e7, mx = 0;
    for (int j = 1; j <= q; j++) {
      int x;
      cin >> x;
      if (mn < x) w[i] = 1;
      mn = min(mn, x);
      mx = max(mx, x);
    }
    if (!w[i]) {
      v.push_back({mn, mx});
    } else {
      ans += n, num++;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (!w[i]) {
      ans += num;
    }
  }
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    d[v[i].first]++;
  }
  for (int i = 1; i <= 1e6; i++) {
    d[i] = d[i - 1] + d[i];
  }
  for (int i = 0; i < v.size(); i++) {
    ans += d[v[i].second - 1];
  }
  cout << ans << endl;
}
