#include #include #include #include #include #include #include using namespace std; using ll = long long; ll solve(const vector>& sgs, const vector>& sgs_inv, const vector>& sgsp, int from, int to) { if (to - from <= 1) return 0; ll ans = 0; int mid = (from + to) / 2; vector> sgsp_a; vector> sgsp_b; for (const auto& p : sgsp) if (p.second < mid) sgsp_a.push_back(p); else sgsp_b.push_back(p); ans += solve(sgs, sgs_inv, sgsp_a, from, mid); ans += solve(sgs, sgs_inv, sgsp_b, mid, to); ll counter = 0; for (const auto& p : sgsp) { if (p.second < mid) ans += counter; if (p.second >= mid) ++counter; } return ans; } int main() { int n; cin >> n; vector> sgsp; vector> sgs(n); vector> sgs_inv(n); for (int i = 0; i < n; ++i) { int cnt; cin >> cnt; sgs[i].resize(cnt); for (auto& y : sgs[i]) { cin >> y; sgs_inv[y].push_back(i); sgsp.emplace_back(y, i); } } sort(sgsp.begin(), sgsp.end()); cout << solve(sgs, sgs_inv, sgsp, 0, n) << '\n'; return 0; }