#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, cnt = 0, mx = 0, k, x;
  cin >> n;
  pair<long long int, long long int> a[200005];
  for (long long int i = 1; i <= n; i++) {
    mx = 0;
    cin >> k;
    for (long long int j = 1; j <= k; j++) {
      cin >> x;
      mx = max(x, mx);
    }
    a[i] = {mx, k};
  }
  sort(a + 1, a + 1 + n);
  for (long long int i = 1; i < n; i++) {
    cnt += (a[n].first - a[i].first) * a[i].second;
  }
  cout << cnt << endl;
  return 0;
}
