#include <bits/stdc++.h>
using namespace std;
long long n;
long long m[200005];
long long mx[200005];
long long ans = 0;
long long mxx = 0;
signed main() {
  scanf("%lld", &n);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &m[i]);
    for (long long j = 1; j <= m[i]; j++) {
      long long x;
      scanf("%lld", &x);
      mx[i] = max(mx[i], x);
    }
    mxx = max(mxx, mx[i]);
  }
  for (long long i = 1; i <= n; i++) {
    ans += (mxx - mx[i]) * m[i];
  }
  printf("%lld", ans);
  return 0;
}
