#include <bits/stdc++.h>
using namespace std;
struct Company {
  long long imployee, maxsala;
} company[200005];
bool cmp(struct Company x, struct Company y) { return x.maxsala > y.maxsala; }
int main(int argc, char *argv[]) {
  long long n, i, j, t, ans = 0;
  struct Company temp;
  scanf("%lld", &n);
  for (i = 0; i < n; i++) {
    scanf("%lld", &company[i].imployee);
    company[i].maxsala = 0;
    for (j = 0; j < company[i].imployee; j++) {
      scanf("%lld", &t);
      if (company[i].maxsala < t) company[i].maxsala = t;
    }
  }
  sort(company, company + n, cmp);
  for (i = 1; i < n; i++)
    ans += (company[0].maxsala - company[i].maxsala) * company[i].imployee;
  printf("%lld\n", ans);
  return 0;
}
