#include <bits/stdc++.h>
int main(void) {
  long long int n, i, j, maxGlobal, temp;
  scanf("%lld", &n);
  long long int m[n], max[n];
  long long int ans;
  for (i = 0; i < n; i++) {
    scanf("%lld", &m[i]);
    for (j = 0, max[i] = 0; j < m[i]; j++) {
      scanf("%lld", &temp);
      if (temp > max[i]) max[i] = temp;
    }
  }
  for (i = 0, maxGlobal = 0; i < n; i++) {
    if (maxGlobal < max[i]) {
      maxGlobal = max[i];
    }
  }
  for (i = 0, ans = 0; i < n; i++) {
    ans += (maxGlobal - max[i]) * m[i];
  }
  printf("%lld\n", ans);
  return 0;
}
