#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, i, j, k, value, count = 0, check = 0, max = 0;
  cin >> n;
  vector<pair<long long, long long> > x;
  set<long long> y;
  vector<long long> z;
  set<long long>::iterator itr;
  for (i = 1; i <= n; i++) {
    cin >> k;
    for (j = 1; j <= k; j++) {
      cin >> value;
      if (value > max) max = value;
    }
    pair<long long, long long> p(max, k);
    x.push_back(p);
    max = 0;
  }
  long long sum = 0;
  sort(x.rbegin(), x.rend());
  value = x[0].first;
  for (i = 1; i < n; i++) {
    sum = sum + (value - x[i].first) * x[i].second;
  }
  cout << sum;
  return 0;
}
