#include <bits/stdc++.h>
using namespace std;
long long t[200005], money[200005];
long long maxx[200005];
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> t[i];
    for (long long j = 0; j < t[i]; j++) cin >> money[j];
    for (long long j = 0; j < t[i]; j++) maxx[i] = max(money[j], maxx[i]);
  }
  long long ans = 0, cnt = 0;
  for (long long i = 0; i < n; i++) ans = max(maxx[i], ans);
  for (long long i = 0; i < n; i++) cnt += (ans - maxx[i]) * t[i];
  cout << cnt;
  return 0;
}
