#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long ull;
int n;
int m;
ll curr;
vector<pair<int, ll> > sizesAndMaxes;
int main() {
  cin >> n;
  int n2 = n;
  ll globalMax = 0;
  while (n--) {
    cin >> m;
    int size = m;
    ll max = -1;
    while (m--) {
      cin >> curr;
      if (curr > max) {
        max = curr;
      }
      if (curr > globalMax) {
        globalMax = curr;
      }
    }
    sizesAndMaxes.push_back(make_pair(size, max));
  }
  ll ans = 0;
  for (int i = 0; i < n2; ++i) {
    ans += (sizesAndMaxes[i].first) * (globalMax - sizesAndMaxes[i].second);
  }
  cout << ans << endl;
}
