#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int vis[n + 1];
  vector<int> _max;
  vector<int> _min;
  _max.clear();
  _min.clear();
  long long _totA = 0;
  int m;
  for (int i = 0; i < n; i++) {
    cin >> m;
    int flag = 0;
    vis[i] = 0;
    long long _xtemp = -1, _ntemp = -1, lowtillnow = -1, temp;
    for (int j = 0; j < m; j++) {
      cin >> temp;
      if (_xtemp == -1 || _xtemp < temp) _xtemp = temp;
      if (_ntemp == -1 || _ntemp > temp) _ntemp = temp;
      if (lowtillnow == -1) lowtillnow = temp;
      if (lowtillnow < temp) {
        flag = 1;
      } else {
        lowtillnow = temp;
      }
    }
    _min.push_back(_ntemp);
    if (flag == 1) {
      _totA++;
      vis[i]++;
    } else
      _max.push_back(_xtemp);
  }
  sort(_max.begin(), _max.end());
  vector<int>::iterator low, up;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i])
      ans += n;
    else {
      ans += _totA;
      up = upper_bound(_max.begin(), _max.end(), _min[i]);
      ans += (_max.size() - (up - _max.begin()));
    }
  }
  cout << ans << endl;
  return 0;
}
