#include <bits/stdc++.h>
using namespace std;
long long ascents = 0;
vector<int> maximos;
vector<int> minimos;
int locmin = 0;
int locmax = 0;
bool isAscent = false;
void readData(int length) {
  isAscent = false;
  int data;
  locmin = INT_MAX;
  locmax = INT_MIN;
  for (int i = 0; i < length; i++) {
    cin >> data;
    if (data > locmin) {
      isAscent = true;
    }
    locmin = min(locmin, data);
    locmax = max(locmax, data);
  }
  if (isAscent) {
    minimos.push_back(INT_MIN);
    maximos.push_back(INT_MAX);
  } else {
    minimos.push_back(locmin);
    maximos.push_back(locmax);
  }
}
int main() {
  int n;
  cin >> n;
  int tamano;
  for (int i = 0; i < n; i++) {
    cin >> tamano;
    readData(tamano);
  }
  sort(maximos.begin(), maximos.end());
  for (int i = 0; i < n; i++) {
    ascents +=
        maximos.end() - upper_bound(maximos.begin(), maximos.end(), minimos[i]);
  }
  cout << ascents << endl;
}
