#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, worker, salary, t = 0, m, a[200000], b[200000], i, j;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> worker;
    b[i] = worker;
    long long m = 0;
    for (j = 1; j <= worker; j++) {
      cin >> salary;
      m = max(salary, m);
    }
    t = max(t, m);
    a[i] = m;
  }
  long long ans = 0;
  for (i = 1; i <= n; i++) {
    ans += (t - a[i]) * b[i];
  }
  cout << ans << endl;
}
