#include <bits/stdc++.h>
using namespace std;
int main() {
  int maxmax = 0, max, x, sum = 0, n, m;
  long long int sumsum = 0;
  scanf("%d", &n);
  if (n == 1) printf("%d\n", 0), exit(0);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &m);
    max = 0;
    for (int j = 0; j < m; ++j) {
      scanf("%d", &x);
      if (x > max) max = x;
    }
    sumsum += (long long int)max * m;
    sum += m;
    if (max > maxmax) maxmax = max;
  }
  printf("%lld\n", (long long int)sum * (long long int)maxmax - sumsum);
}
