#include <bits/stdc++.h>
#pragma optimize("O3")
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 2 * 1e9 + 7;
const int base = 2e5 + 1;
const int MAX = 1e5 + 2;
const double EPS = 1e-9;
const double PI = acos(-1.);
const int MAXN = 2 * 1e6 + 1;
int main() {
  int n;
  cin >> n;
  int k = 0;
  vector<pair<bool, int>> x;
  vector<int> maxi;
  for (long long i = (0); i < (n); ++i) {
    int l;
    cin >> l;
    int el;
    int m, mini;
    cin >> m;
    mini = m;
    el = m;
    bool is = false;
    for (long long j = (1); j < (l); ++j) {
      cin >> m;
      if (m > mini) {
        is = true;
      } else {
        mini = m;
      }
    }
    x.push_back({is, mini});
    if (is) {
      k++;
    } else {
      maxi.push_back(el);
    }
  }
  sort(maxi.begin(), maxi.end());
  long long result = 0;
  for (long long i = (0); i < (n); ++i) {
    if (x[i].first) {
      result += n;
    } else {
      int l = 0, r = maxi.size() - 1;
      int m = (l + r) / 2;
      while (r - l > 1) {
        if (maxi[m] <= x[i].second) {
          l = m + 1;
        } else {
          r = m;
        }
        m = (l + r) / 2;
      }
      if (maxi[l] > x[i].second) {
        r = l;
      }
      if (maxi[r] > x[i].second) {
        result += maxi.size() - r;
      }
      result += k;
    }
  }
  cout << result;
  return 0;
}
