#include <bits/stdc++.h>
using namespace std;
vector<int> v[100005];
int first[100005];
int last[100005];
bool good[100005];
vector<int> starts;
int main() {
  int N;
  cin >> N;
  int good_cnt = 0;
  for (int i = 1; i <= N; i++) {
    int l;
    cin >> l;
    for (int j = 0; j < l; j++) {
      int x;
      cin >> x;
      if (v[i].size() && x > v[i].back()) good[i] = true;
      v[i].push_back(x);
    }
    first[i] = v[i][0];
    last[i] = v[i].back();
    if (good[i]) good_cnt++;
  }
  long long ans = 0;
  for (int i = 1; i <= good_cnt; i++) ans += (N - i) * 2 + 1;
  for (int i = 1; i <= N; i++)
    if (!good[i]) starts.push_back(first[i]);
  sort(starts.begin(), starts.end());
  for (int i = 1; i <= N; i++)
    if (!good[i])
      ans +=
          starts.size() -
          (upper_bound(starts.begin(), starts.end(), last[i]) - starts.begin());
  cout << ans;
}
