#include using namespace std; using ll = long long; using ull = unsigned long long; ll bum(vector& ts, int f, int t) { if(t - f <= 1) return 0; vector left; left.reserve(t-f); vector right; left.reserve(t-f); 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, 0, 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> qs(n); for(int i = 0; i < n; ++i) { cin >> ps[i]; qs[i].resize(ps[i]); for(int j = 0; j < ps[i]; ++j) { cin >> qs[i][j]; } sort(begin(qs[i]), end(qs[i])); m += ps[i]; } vector fs; for(int i = 0; i < n; ++i) { for(int j = 0; j < ps[i]; ++j) { fs.push_back(qs[i][j]); } } cout << bum(fs, 0, n) << '\n'; return 0; }