#include <bits/stdc++.h>
using namespace std;
long long int dp[1000001][2];
void solve() {
  long long int n;
  cin >> n;
  long long int ans = 0;
  vector<long long int> ppp;
  for (long long int i = 1; i <= n; i++) {
    long long int maxi = INT_MIN, mini = INT_MAX;
    long long int l;
    cin >> l;
    bool f = 0;
    for (long long int j = 1, xx; j <= l; j++) {
      cin >> xx;
      if (xx > mini) {
        f = 1;
      }
      maxi = max(maxi, xx);
      mini = min(maxi, xx);
    }
    dp[i][0] = dp[i][0] = -1;
    if (f == 0) {
      dp[i][0] = mini;
      dp[i][1] = maxi;
      ppp.push_back(maxi);
    }
  }
  sort(ppp.begin(), ppp.end());
  for (long long int i = 1; i <= n; i++) {
    if (dp[i][0] != -1) {
      long long int idx =
          upper_bound(ppp.begin(), ppp.end(), dp[i][0]) - ppp.begin();
      ans = ans + idx;
      if (idx == (long long int)ppp.size()) {
      }
    }
  }
  ans = n * n - (ans);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  {
    solve();
    cout << "\n";
  }
}
