#include <bits/stdc++.h>
int main() {
  int n;
  int m[200020];
  int MaxS[200020];
  std::cin >> n;
  int emax = 0;
  for (int i = 0; i < n; i++) {
    std::cin >> m[i];
    int s = 0, max = 0;
    for (int t = 0; t < m[i]; t++) {
      std::cin >> s;
      if (s > max) max = s;
    }
    MaxS[i] = max;
    if (MaxS[i] > emax) emax = MaxS[i];
  }
  long long result = 0;
  for (int i = 0; i < n; i++) {
    result += (long long)(emax - MaxS[i]) * m[i];
  }
  std::cout << result;
  return 0;
}
