#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e9;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  long long temp = n;
  long long max_arr[n];
  long long number[n];
  long long counter = 0;
  while (temp--) {
    long long m;
    cin >> m;
    long long brr[m];
    long long max = 0;
    for (long long i = 0; i < m; i++) {
      cin >> brr[i];
      if (brr[i] > max) max = brr[i];
    }
    number[counter] = m;
    max_arr[counter] = max;
    counter++;
  }
  long long maxmax = 0;
  for (long long i = 0; i < n; i++) {
    if (maxmax < max_arr[i]) maxmax = max_arr[i];
  }
  long long sum = 0;
  for (long long i = 0; i < n; i++) {
    sum = sum + ((maxmax - max_arr[i]) * number[i]);
  }
  cout << sum << endl;
}
