#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int32_t N;
  cin >> N;
  uint32_t asc_cnt = 0;
  vector<pair<int32_t, int32_t> > desc;
  desc.reserve(N);
  for (int32_t i = 0; i < N; ++i) {
    size_t _size;
    cin >> _size;
    vector<int32_t> in(_size);
    for (int32_t &i : in) cin >> i;
    bool flag = 0;
    for (size_t i = 1; i < _size; ++i)
      if (in[i - 1] < in[i]) flag = 1;
    if (flag)
      ++asc_cnt;
    else
      desc.emplace_back(*min_element(in.begin(), in.end()),
                        *max_element(in.begin(), in.end()));
  }
  uint64_t ans = asc_cnt * asc_cnt + 2 * asc_cnt * desc.size();
  sort(
      desc.begin(), desc.end(),
      [](const pair<int32_t, int32_t> &lhs, const pair<int32_t, int32_t> &rhs) {
        return lhs.second < rhs.second;
      });
  for (size_t i = 0; i < desc.size(); ++i) {
    int32_t l = -1, r = desc.size();
    while (r - l > 1) {
      int32_t mid = l + r >> 1;
      if (desc[i].first < desc[mid].second)
        r = mid;
      else
        l = mid;
    }
    ans +=
        desc.size() - r - (r <= i && desc[i].first == desc[i].second ? 1 : 0);
  }
  cout << ans;
  return 0;
}
