#include <bits/stdc++.h>
using namespace std;
int main() {
  int k;
  cin >> k;
  vector<pair<long long, long long> > data;
  long long m = 0;
  for (int i = 0; i < k; i++) {
    long long n;
    cin >> n;
    long long temp = 0;
    for (int i = 0; i < n; i++) {
      long long g;
      cin >> g;
      temp = max(temp, g);
    }
    data.push_back({temp, n});
    m = max(m, temp);
  }
  long long sol = 0;
  for (vector<pair<long long, long long> >::iterator itr = data.begin();
       itr != data.end(); itr++)
    sol += (m - (*itr).first) * (*itr).second;
  cout << sol << endl;
  return 0;
}
