#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int maxmax = 0, max, x, sum = 0, n, m, count = 0;
  scanf("%lld", &n);
  pair<long long int, long long int> a[n];
  for (int i = 0; i < n; ++i) {
    scanf("%lld", &m);
    max = 0;
    for (int j = 0; j < m; ++j) {
      scanf("%lld", &x);
      if (x > max) max = x;
    }
    a[i] = make_pair(max, m);
    if (max > maxmax) maxmax = max;
  }
  for (int i = 0; i < n; ++i) {
    sum += (maxmax - a[i].first) * a[i].second;
  }
  printf("%lld\n", sum);
}
