#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, max = 0, mx = 0, j, sum = 0, d, m;
  vector<pair<int, int> > a;
  vector<pair<int, int> >::iterator it;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> m;
    max = 0;
    for (j = 0; j < m; j++) {
      cin >> d;
      if (max < d) max = d;
    }
    if (mx < max) mx = max;
    a.push_back(make_pair(m, max));
  }
  for (it = a.begin(); it < a.end(); it++) {
    sum += (mx - it->second) * it->first;
  }
  cout << sum << endl;
}
