#include using namespace std; using ll = long long; #define dbl long double #define vec vector #define um unordered_map #define us unordered_set using ull = unsigned long long; using vi = vec; using vl = vector; using vd = vector; using pii = pair; using pll = pair; using pdd = pair; void solve(int n){ vector st(n); vector dt(n); vector ps(n); for (int i = 0; i < n; i++) { int t; cin >> t; for (int j = 0; j < t; j++) { int x; cin >> x; st[i].push_back(x); if (i > x) { dt[x].second++; } else if (i == x) { dt[x].first++; } } } for (int i = 0; i < n; i++) { ps[i].first = dt[i].first + (i == 0 ? 0 : ps[i -1].first); ps[i].second = dt[i].second + (i == 0 ? 0 : ps[i -1].second); } int cnt = 0; for (int i = 0; i < n; i++) { for (auto j : st[i]) { int dig, row; if (i < j) { dig = (j == 0 ? 0 : ps[j - 1].second) - (ps[i].second); row = (j == 0 ? 0 : ps[j - 1].first) - (i + 1 <= j ? 0 : ps[i + 1].first); } cnt += dig + row; } } cout << cnt << endl; } int main(){ cin.sync_with_stdio(false); cin.tie(0); cin.exceptions(ios::failbit); int n; cin >> n; solve(n); return EXIT_SUCCESS; }