#include using namespace std; #define rep(i, a, b) for(int i = a; i < (b); ++i) #define all(x) begin(x), end(x) #define sz(x) (int)(x).size() typedef long long ll; typedef pair pii; typedef vector vi; typedef vector> vvi; typedef vector> vpii; template using vec = vector; template using uset = unordered_set; template using umap = unordered_map; struct FT { vector s; FT(int n) : s(n) {} void update(int pos, ll dif) { // a[pos] += diff pos = s.size() - pos - 1; for (; pos < sz(s); pos |= pos + 1) s[pos] += dif; } ll query(int pos) { pos = s.size() - pos - 1; ll res = 0; for (; pos > 0; pos &= pos - 1) res += s[pos - 1]; return res; } }; void solve () { int n; cin >> n; vpii birds; vi endCounts(n + 1); rep(x, 0, n) { int c; cin >> c; rep(_, 0, c) { int y; cin >> y; birds.emplace_back(x + 1, y + 1); endCounts[y + 1]++; } } sort(all(birds)); rep(i, 1, n + 1) endCounts[i] += endCounts[i - 1]; FT endSufSum(n + 1); ll colls = 0; rep(i, 0, sz(birds)) { auto [x, y] = birds[i]; ll endBefore = endCounts[y-1], handled = i, handledEndAfterOrEq = endSufSum.query(y-1); colls += endBefore - (handled - handledEndAfterOrEq); endSufSum.update(y, 1); } cout << colls << endl; } int main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); ll T = 1; //cin >> T; rep(i, 0, T) solve(); return 0; }