#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  int mark[t];
  memset(mark, 0, sizeof(mark));
  vector<pair<int, int> > kaif;
  int cnt = 0;
  for (int k = 0; k < t; k++) {
    int n;
    cin >> n;
    int p = INT_MAX, q = INT_MIN;
    for (int i = 0; i < n; i++) {
      int y;
      cin >> y;
      if (y > p) mark[k] = 1;
      p = min(y, p);
      q = max(q, y);
    }
    kaif.push_back({p, q});
  }
  for (int i = 0; i < t; i++)
    if (mark[i]) cnt++;
  vector<int> sol;
  for (int i = 0; i < kaif.size(); i++) {
    if (mark[i] == 0) sol.push_back(kaif[i].second);
  }
  sort(sol.begin(), sol.end());
  long long int ans = 0;
  for (int i = 0; i < kaif.size(); i++) {
    if (mark[i])
      ans += kaif.size();
    else {
      ans += cnt;
      int p = kaif[i].first;
      vector<int>::iterator it = upper_bound(sol.begin(), sol.end(), p);
      ans += (sol.end() - it);
    }
  }
  cout << ans << endl;
  return 0;
}
