#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<vector<int>> v;
  for (int i = 0; i < (n); ++i) {
    int l;
    cin >> l;
    vector<int> temp;
    for (int j = 0; j < (l); ++j) {
      int ele;
      cin >> ele;
      temp.push_back(ele);
    }
    v.push_back(temp);
  }
  vector<int> maxi;
  int cnt_no_ascent = 0;
  bool ascent[n];
  int mini_arr[n];
  int maxi_arr[n];
  for (int i = 0; i < (n); ++i) ascent[i] = false;
  for (int i = 0; i < (n); ++i) {
    int flag = 0;
    int maxi_el = INT_MIN;
    int mini_el = INT_MAX;
    for (int j = 0; j < (v[i].size()); ++j) {
      if (v[i][j] > maxi_el) {
        maxi_el = v[i][j];
      }
      if (v[i][j] < mini_el) {
        mini_el = v[i][j];
      }
      if (j != 0 && v[i][j - 1] < v[i][j]) {
        ++cnt_no_ascent;
        flag = 1;
        ascent[i] = true;
        break;
      }
    }
    if (flag == 0) {
      mini_arr[i] = mini_el;
      maxi.push_back(maxi_el);
      maxi_arr[i] = maxi_el;
    }
  }
  long long ans = ((long long)cnt_no_ascent * (long long)n);
  sort(maxi.begin(), maxi.end());
  for (int i = 0; i < (n); ++i) {
    if (ascent[i] == true) continue;
    auto it = upper_bound(maxi.begin(), maxi.end(), mini_arr[i]);
    ans = ans + (long long)(maxi.end() - it);
    ans = ans + cnt_no_ascent;
  }
  cout << ans << endl;
  return 0;
}
