#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, x, curr_max, res = 0;
  cin >> n;
  vector<pair<long long, long long>> v(n);
  for (long long i = 0; i < n; i++) {
    curr_max = -1;
    cin >> m;
    for (long long j = 0; j < m; j++) {
      cin >> x;
      if (x > curr_max) curr_max = x;
    }
    v[i].first = curr_max;
    v[i].second = m;
  }
  sort(v.begin(), v.end());
  for (long long i = 0; i < n - 1; i++)
    res += (v[n - 1].first - v[i].first) * v[i].second;
  cout << res;
}
