#include <bits/stdc++.h>
using namespace std;
long long n, m, a[1000001], mx[1000001], cnt, maxi, ans;
int main() {
  cin >> n;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
    mx[i] = -1;
    for (long long j = 1; j <= a[i]; ++j) {
      long long k;
      cin >> k;
      mx[i] = max(mx[i], k);
    }
  }
  for (long long i = 1; i <= n; ++i) {
    maxi = max(maxi, mx[i]);
  }
  for (long long i = 1; i <= n; ++i) {
    cnt += (maxi - mx[i]) * a[i];
  }
  cout << cnt;
}
