#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, a;
  cin >> n;
  vector<pair<long long, long long>> mxa(n);
  for (int i = 0; i < n; ++i) {
    cin >> mxa[i].first;
    long long res = 0;
    for (int j = 0; j < mxa[i].first; ++j) {
      cin >> a;
      res = max(res, a);
    }
    mxa[i].second = res;
  }
  long long mx = 0;
  for (int i = 0; i < n; ++i) {
    mx = max(mx, mxa[i].second);
  }
  long long ans = 0;
  for (int i = 0; i < n; ++i) ans += (mx - mxa[i].second) * mxa[i].first;
  cout << ans;
  return 0;
}
