#include <bits/stdc++.h>
int main() {
  long long n;
  scanf("%lld", &n);
  std::vector<std::pair<long long, long long> > v(n);
  long long mmx(0);
  for (long long p = 0; p < n; p++) {
    long long m;
    scanf("%lld", &m);
    v[p].first = m;
    long long mx(0);
    for (long long u = 0; u < m; u++) {
      long long x;
      scanf("%lld", &x);
      mx = (mx > x) ? mx : x;
    }
    v[p].second = mx;
    mmx = (mmx > mx) ? mmx : mx;
  }
  long long sum(0);
  for (long long p = 0; p < n; p++) {
    sum += (mmx - v[p].second) * v[p].first;
  }
  printf("%lld\n", sum);
  return 0;
}
