#include <bits/stdc++.h>
using namespace std;
void getint(int &v) {
  char ch, fu = 0;
  for (ch = '*'; (ch < '0' || ch > '9') && ch != '-'; ch = getchar())
    ;
  if (ch == '-') fu = 1, ch = getchar();
  for (v = 0; ch >= '0' && ch <= '9'; ch = getchar()) v = v * 10 + ch - '0';
  if (fu) v = -v;
}
int main() {
  int maxmax = 0, max, x, sum = 0, n, m;
  long long int sumsum = 0;
  getint(n);
  if (n == 1) printf("%d\n", 0), exit(0);
  for (int i = 0; i < n; ++i) {
    getint(m);
    max = 0;
    for (int j = 0; j < m; ++j) {
      getint(x);
      if (x > max) max = x;
    }
    sumsum += (long long)max * m;
    sum += m;
    if (max > maxmax) maxmax = max;
  }
  printf("%lld\n", (long long)sum * (long long)maxmax - sumsum);
}
