#include <bits/stdc++.h>
using namespace std;
const int N = 1000001;
int a[N + 1];
int get(int l, int r) {
  int ans = a[r];
  if (l > 0) ans -= a[l - 1];
  return ans;
}
void solve() {
  int n;
  cin >> n;
  long long ans = 0;
  vector<int> mins;
  for (int i = 0; i < n; i++) {
    int l, s;
    cin >> l;
    cin >> s;
    int mn = s, mx = s;
    bool as = false;
    for (int i = 1; i < l; i++) {
      cin >> s;
      if (s > mn) as = true;
      mn = min(s, mn);
      mx = max(s, mx);
    }
    if (!as) {
      a[mx]++;
      mins.push_back(mn);
    } else {
      ans += n;
      a[N]++;
    }
  }
  for (int i = 1; i <= N; i++) {
    a[i] += a[i - 1];
  }
  for (int j : mins) {
    ans += get(j + 1, N);
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  getchar();
  getchar();
}
