#include <bits/stdc++.h>
using namespace std;
bool compare(pair<long long, long long> a, pair<long long, long long> b) {
  return a.second < b.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long m, x, maxSalary;
  pair<long long, long long> *input = new pair<long long, long long>[n];
  for (long long i = 0; i < n; i++) {
    cin >> m;
    maxSalary = 0;
    for (long long j = 0; j < m; j++) {
      cin >> x;
      maxSalary = max(x, maxSalary);
    }
    input[i] = make_pair(m, maxSalary);
  }
  sort(input, input + n, compare);
  long long workers = 0;
  long long total = 0;
  for (long long i = 0; i < n - 1; i++) {
    workers += input[i].first;
    total += workers * (input[i + 1].second - input[i].second);
  }
  cout << total << endl;
}
