#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> max1(n, 0), m(n);
  int max2 = 0;
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    for (int j = 0; j < m[i]; j++) {
      int x;
      cin >> x;
      max1[i] = max(max1[i], x);
    }
    max2 = max(max1[i], max2);
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) sum += 1ll * (max2 - max1[i]) * m[i];
  cout << sum << endl;
  return 0;
}
