#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5 + 1;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long LINF = 1e18;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long tc;
  cin >> tc;
  vector<long long> last, first;
  long long x = 0;
  for (int t = 1; t <= tc; t++) {
    long long l;
    cin >> l;
    long long a[l];
    for (long long i = 0; i < l; i++) {
      cin >> a[i];
    }
    if (is_sorted(a, a + l, greater<long long>())) {
      first.push_back(a[0]);
      last.push_back(a[l - 1]);
    }
  }
  sort(first.begin(), first.end());
  std::vector<long long>::iterator v;
  long long ans = tc * tc;
  for (long long i = 0; i < last.size(); i++) {
    v = upper_bound(first.begin(), first.end(), last[i]);
    ans -= (v - first.begin());
  }
  cout << ans << endl;
  return 0;
}
