#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  pair<long long, long long> b[n + 1];
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    long long a[x];
    for (long long j = 0; j < x; j++) cin >> a[j];
    sort(a, a + x);
    b[i] = make_pair(a[x - 1], x);
  }
  sort(b, b + n);
  long long k = 0;
  long long res = 0;
  while (b[k].first != b[n - 1].first && k < n - 1) {
    res += (b[n - 1].first - b[k].first) * b[k].second;
    k++;
  }
  cout << res;
  return 0;
}
