#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int INFMEM = 63;
const int INF = 1061109567;
const long long LINF = 4557430888798830399LL;
const double DINF = numeric_limits<double>::infinity();
const long long MOD = 1000000007;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
const double PI = 3.141592653589793;
inline void open(string a) {
  freopen((a + ".in").c_str(), "r", stdin);
  freopen((a + ".out").c_str(), "w", stdout);
}
inline void fasterios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long cnt[1000005];
pair<long long, long long> isi[100005];
bool isAscent[100005];
int main() {
  fasterios();
  long long n;
  cin >> n;
  long long cntascent = 0;
  for (int i = 1; i <= n; i++) {
    long long k;
    cin >> k;
    pair<long long, long long> cur;
    long long pre = -1;
    bool ascent = 0;
    for (int j = 1; j <= k; j++) {
      long long tmp;
      cin >> tmp;
      if (j == 1) {
        cur.first = cur.second = tmp;
        pre = tmp;
      } else {
        if (pre < tmp) ascent = 1;
        pre = tmp;
      }
      cur.first = min(cur.first, tmp);
      cur.second = max(cur.second, tmp);
    }
    isi[i] = cur;
    if (!ascent) cnt[cur.first]++;
    isAscent[i] = ascent;
    cntascent += ascent;
  }
  for (int i = 0; i <= 1000000; i++) cnt[i] += cnt[i - 1];
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    if (isAscent[i]) {
      ans += n;
      continue;
    }
    long long now = isi[i].second;
    ans += cnt[now - 1];
    ans += cntascent;
  }
  cout << ans << '\n';
  return 0;
}
