#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
int n, i, l, minn, maxx, ok, a, okC, j;
vector<int> vMax, vMin;
long long res;
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &l);
    minn = INF;
    maxx = -INF;
    ok = 0;
    while (l--) {
      scanf("%d", &a);
      if (a > minn) ok = 1;
      minn = min(minn, a);
      maxx = max(maxx, a);
    }
    if (!ok) {
      vMax.push_back(maxx);
      vMin.push_back(minn);
    } else
      okC++;
  }
  res = okC * 1LL * okC + okC * 2LL * (n - okC);
  j = 0;
  sort(vMin.begin(), vMin.end());
  sort(vMax.begin(), vMax.end());
  for (i = 0; i < vMin.size(); i++) {
    while (j < vMax.size() && vMax[j] <= vMin[i]) j++;
    res += (vMax.size() - j);
  }
  printf("%lld\n", res);
  return 0;
}
