#include <bits/stdc++.h>
int main() {
  int64_t n;
  std::cin >> n;
  std::vector<int64_t> mins, maxs;
  int64_t rising = 0;
  for (int64_t i = 0; i < n; i++) {
    int64_t m;
    int64_t min = 1e7, max = -1;
    bool rise = false;
    std::cin >> m;
    for (int64_t j = 0; j < m; j++) {
      int64_t x;
      std::cin >> x;
      if (min < x) {
        rise = true;
      }
      min = std::min(min, x);
      max = std::max(max, x);
    }
    if (rise) {
      rising++;
    } else {
      mins.push_back(min);
      maxs.push_back(max);
    }
  }
  std::sort(maxs.begin(), maxs.end());
  int64_t ans = rising * rising + rising * maxs.size() * 2;
  for (auto min : mins) {
    auto u_b = std::upper_bound(maxs.begin(), maxs.end(), min);
    ans += std::distance(u_b, maxs.end());
  }
  std::cout << ans;
}
