#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m;
  cin >> n;
  vector<pair<long long, long long> > v;
  long long top = 0;
  while (n--) {
    cin >> m;
    long long arr[m];
    long long mx = 0;
    for (int i = 0; i < m; i++) {
      cin >> arr[i];
      mx = max(mx, arr[i]);
    }
    v.push_back(pair<long long, long long>(mx, m));
    top = max(top, mx);
  }
  sort(v.begin(), v.end());
  long long ans = 0;
  for (auto it = v.begin(); it != v.end(); it++) {
    long long dif = top - it->first;
    ans += dif * (it->second);
  }
  cout << ans << endl;
}
