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