#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int maxmax = 0, max, x, sum = 0, sumsum = 0, n, m;
  scanf("%lld", &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;
    }
    sumsum += max * m;
    sum += m;
    if (max > maxmax) maxmax = max;
  }
  printf("%lld\n", sum * maxmax - sumsum);
}
