#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int maxn = 2e6 + 43;
const int maxm = 2e3 + 43;
const int maxlog = 30;
const long long mod = (long long)1e9 + 7;
const int sq = 340;
const int inf = 1e9;
const long double PI = 3.141592653589793238463;
int a[maxn];
int cnt[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  int n;
  cin >> n;
  vector<int> v;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    bool flag = false;
    for (int j = 0; j < x; j++) {
      cin >> a[j];
      if (j && (a[j] > a[j - 1])) flag = true;
    }
    if (flag) continue;
    int mini = a[x - 1];
    int maxi = a[0];
    v.push_back(mini);
    cnt[maxi]++;
  }
  for (int i = 1; i < maxn; i++) {
    cnt[i] += cnt[i - 1];
  }
  for (int i = 0; i < v.size(); i++) {
    int cur = v[i];
    ans += (long long)cnt[cur];
  }
  cout << (long long)n * (long long)n - ans;
  return 0;
}
