#include <bits/stdc++.h>
using namespace std;
long long i, n, a[200001], b[200001], j, k, maxx, maxn, ans;
int main() {
  cin >> n;
  for (i = 0; i < n; i++) {
    maxx = 0;
    cin >> a[i];
    for (j = 0; j < a[i]; j++) {
      cin >> k;
      if (k >= maxx) maxx = k, b[i] = maxx;
    }
    if (maxn <= maxx) maxn = maxx;
  }
  for (i = 0; i < n; i++) {
    ans += (maxn - b[i]) * a[i];
  }
  cout << ans;
}
