#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long c0 = 0, c1 = 0, ans = 0;
  vector<int> st, en;
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int l, s, e, p = 1e9;
    cin >> l;
    bool flag = true;
    for (int j = 0; j < l; ++j) {
      int x;
      cin >> x;
      if (p < x) flag = false;
      if (j == 0) s = x;
      if (j == l - 1) e = x;
      p = x;
    }
    if (flag)
      st.push_back(s), en.push_back(e), c0++;
    else
      c1++;
  }
  sort(st.begin(), st.end());
  sort(en.begin(), en.end());
  ans = c1 * (c1 + 2 * c0);
  for (int i = 0; i < st.size(); ++i) {
    int k = lower_bound(en.begin(), en.end(), st[i]) - en.begin();
    if (k) ans += k;
  }
  cout << ans << "\n";
}
