#include <bits/stdc++.h>   using namespace std;   int tc = 1;   const int M = 1e9 + 7;   const long long Inf = (long long)2e18 + 5;   const int N = 2e5 + 5;   std::vector<long long> a[N];   int solve() {     int a;     cin >> a;     int z = 0;     int bg = 0;     int xx = 0;     int mx = 0;     long long ans = 0LL;     for (z = 0; z < a; z++) {       int n;       cin >> n;       mx = 0;       for (int i = 0; i < n; i++) {         int x;         cin >> x;         mx = max(mx, x);       }       if (z > 0) {         if (xx > mx) {           ans += (long long)(xx - mx) * n;           bg += n;         } else {           ans += (long long)(mx - xx) * bg;           xx = mx;           bg += n;         }       } else {         xx = mx;         bg += n;       }     }     cout << ans << endl;     return 0;   }   int main() {     int test = 1;     while (test--) solve();     return 0;   }   