#include using namespace std; using ll = long long; using ull = unsigned long long; ll bum(vector& ts, int f, int t) { if(ts.size() <= 1) return 0; if(t - f <= 1) return 0; vector left; left.reserve(ts.size()/2); vector right; right.reserve(ts.size()/2); int m = (f+t)/2; ll sm = 0; for(int a : ts) { if(a < m) { left.push_back(a); sm += right.size(); } else right.push_back(a); } sm += bum(left, f, m); sm += bum(right, m, t); return sm; } int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; int m = 0; vector ps(n); vector fs; for(int i = 0; i < n; ++i) { cin >> ps[i]; vector qs; qs.resize(ps[i]); for(int j = 0; j < ps[i]; ++j) { cin >> qs[j]; } sort(begin(qs), end(qs)); m += ps[i]; for(int j = 0; j < ps[i]; ++j) { fs.push_back(qs[j]); } } cout << bum(fs, 0, n) << '\n'; return 0; }