#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 200000;
int ms[MAX_N], as[MAX_N];
int main() {
  int n;
  scanf("%d", &n);
  int maxa = 0;
  for (int i = 0; i < n; i++) {
    as[i] = 0;
    scanf("%d", ms + i);
    for (int j = 0; j < ms[i]; j++) {
      int aj;
      scanf("%d", &aj);
      as[i] = max(as[i], aj);
    }
    maxa = max(maxa, as[i]);
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) sum += (long long)ms[i] * (maxa - as[i]);
  printf("%lld\n", sum);
  return 0;
}
