#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(nullptr);
  long long n;
  cin >> n;
  vector<pair<long long, long long>> a(n);
  for (long long i = 0; i < n; i++) {
    long long k, maxx = 0;
    cin >> k;
    a[i].second = k;
    for (long long j = 0; j < k; j++) {
      long long b;
      cin >> b;
      maxx = max(maxx, b);
    }
    a[i].first = maxx;
  }
  sort(a.begin(), a.end(), greater<pair<long long, long long>>());
  long long maxx = a[0].first, ans = 0;
  for (long long i = 1; i < n; i++) {
    ans += a[i].second * (maxx - a[i].first);
  }
  cout << ans;
}
