#include <bits/stdc++.h>
using namespace std;
std::vector<long long> l, f;
long long binarySearch(long long l, long long r, long long x) {
  if (r >= l) {
    long long mid = l + (r - l) / 2;
    if (f[mid] <= x && f[mid + 1] > x) return mid;
    if (f[mid] > x) return binarySearch(l, mid - 1, x);
    return binarySearch(mid + 1, r, x);
  }
  return -1;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, len, ele, ans = 0;
  cin >> n;
  std::vector<long long> s[n + 1];
  for (long long i = 1; i <= n; i++) {
    long long ne = 1;
    cin >> len;
    cin >> ele;
    s[i].push_back(ele);
    for (long long j = 1; j < len; j++) {
      cin >> ele;
      s[i].push_back(ele);
      if (s[i][j] > s[i][j - 1]) ne = 0;
    }
    if (ne == 1) {
      l.push_back(s[i][len - 1]);
      f.push_back(s[i][0]);
    }
  }
  sort(f.begin(), f.end());
  f.push_back(1e9);
  for (long long i = 0; i < l.size(); i++) {
    long long add = binarySearch(0, f.size() - 1, l[i]);
    ans += add + 1;
  }
  cout << n * n - ans;
  return (0);
}
