#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
const int OO = 2e9;
const long long INF = (long long)9e18;
int n, l;
int tmp, curmin, curmax;
int ascent;
bool naik;
vector<pair<int, int> > ins, ins2;
int main() {
  scanf("%d", &n);
  ascent = 0;
  ins.clear();
  ins2.clear();
  for (int z = 1; z <= n; z += 1) {
    scanf("%d", &l);
    naik = false;
    curmin = OO;
    curmax = 0;
    for (int x = 1; x <= l; x += 1) {
      scanf("%d", &tmp);
      if (tmp > curmin) {
        naik = 1;
      }
      curmin = min(curmin, tmp);
      curmax = max(curmax, tmp);
    }
    if (naik) {
      ascent++;
    } else {
      ins.push_back(make_pair(curmin, z));
      ins2.push_back(make_pair(curmax, z));
    }
  }
  sort(ins.begin(), ins.end());
  sort(ins2.begin(), ins2.end());
  int i = ins.size() - 1;
  long long hasil = 0;
  for (int j = ins2.size() - 1; j >= 0; j--) {
    while (i >= 0 && (ins[i].first >= ins2[j].first)) {
      i--;
    }
    hasil += (i + 1);
  }
  i = ins.size();
  for (int z = 1; z <= ascent; z += 1) {
    hasil += 2 * i;
  }
  hasil += (long long)ascent * (long long)ascent;
  printf("%lld\n", hasil);
  return 0;
}
