#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n;
  vector<pair<long long, long long>> jobs;
  long long ma, num, totalma = 0;
  for (int i = 0; i < n; i++) {
    ma = 0;
    cin >> m;
    for (int j = 0; j < m; j++) {
      cin >> num;
      ma = max(ma, num);
    }
    totalma = max(totalma, ma);
    jobs.push_back(make_pair(m, ma));
  }
  long long ans = 0, sum;
  for (int i = 0; i < jobs.size(); i++)
    ans += jobs[i].first * (totalma - jobs[i].second);
  cout << ans;
  return 0;
}
