#include <bits/stdc++.h>
using namespace std;
vector<long long> company[200005];
long long Max[200005];
int main() {
  long long N;
  cin >> N;
  long long global_max = 0;
  for (long long i = 1; i <= N; i++) {
    long long M;
    cin >> M;
    for (long long j = 0; j < M; j++) {
      long long x;
      cin >> x;
      company[i].push_back(x);
      Max[i] = max(Max[i], x);
      global_max = max(global_max, Max[i]);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= N; i++)
    ans += (global_max - Max[i]) * company[i].size();
  cout << ans;
}
