#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b) {
  a = a % 998244353;
  if (b == 0) return 1LL;
  if (b == 1) return a;
  long long int temp = power(a, b / 2);
  if (b % 2 == 0) return (temp * temp) % 998244353;
  return (((a * temp) % 998244353) * temp) % 998244353;
}
void dfs(vector<vector<long long int> > &graph, int mark[], long long int i) {
  mark[i] = 1;
  for (long long int j = 0; j < (long long int)graph[i].size(); j++)
    if (mark[graph[i][j]] == 0) dfs(graph, mark, graph[i][j]);
}
int check(string b, long long int i, string a) {
  if ((long long int)b.length() - i < (long long int)a.length()) return 0;
  int mark1[26];
  int mark2[26];
  memset(mark1, 0, sizeof(mark1));
  memset(mark2, 0, sizeof(mark2));
  for (long long int j = 0; j < (long long int)a.length(); j++) {
    mark1[a[j] - 'a']++;
    mark2[b[j + i] - 'a']++;
  }
  for (long long int j = 0; j < 26; j++) {
    if (mark1[j] != mark2[j]) return 0;
  }
  return 1;
}
void invert(string s, long long int i, long long int j) {
  for (long long int k = i; k < j + 1; k++) s[k] = (char)('1' - s[i] + '0');
}
long long int check(string &s) {
  long long int i, c = 1, ans = 0;
  for (i = 1; i < (long long int)s.length(); i++) {
    if (s[i] == s[i - 1])
      c++;
    else
      ans += (c - 1), c = 1;
  }
  ans += c - 1;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, cnt = 0;
  cin >> n;
  int mark[n], i;
  memset(mark, 0, sizeof(mark));
  vector<vector<long long int> > v(n);
  vector<long long int> vv;
  for (i = 0; i < n; i++) {
    int l, j;
    cin >> l;
    for (j = 0; j < l; j++) {
      long long int temp;
      cin >> temp;
      v[i].push_back(temp);
    }
    long long int minn = v[i][0];
    for (j = 1; j < l; j++) {
      if (v[i][j] > minn) mark[i] = 1;
      minn = min(minn, v[i][j]);
    }
    if (mark[i] == 0)
      vv.push_back(v[i][0]);
    else
      cnt++;
  }
  sort(vv.begin(), vv.end());
  long long int an = 0;
  for (i = 0; i < n; i++) {
    if (mark[i])
      an += n;
    else {
      an += cnt;
      long long int ind =
          upper_bound(vv.begin(), vv.end(), v[i].back()) - vv.begin();
      an += (vv.size() - ind);
    }
  }
  cout << an << endl;
}
