#include <bits/stdc++.h>
using namespace std;
int n, len, temp, ax[100015], in[100015], ans[(int)1e6 + 15];
long long sum;
int main() {
  scanf("%d", &n);
  memset(in, 0x3f, sizeof(in));
  for (int i = 1; i <= n; ++i) {
    scanf("%d", &len);
    bool flag = 0;
    for (int j = 1; j <= len; ++j) {
      scanf("%d", &temp);
      if (temp > in[i] && j > 1) flag = 1;
      ax[i] = max(ax[i], temp);
      in[i] = min(in[i], temp);
    }
    if (flag) in[i] = -1, ax[i] = (int)1e6;
    ans[in[i] + 1]++;
  }
  for (int i = 1; i <= (int)1e6; ++i) ans[i] = ans[i - 1] + ans[i];
  for (int i = 1; i <= n; ++i) sum += ans[ax[i]];
  printf("%lld\n", sum);
  return 0;
}
