#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long double pi = acos(-1);
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie();
  long long t;
  cin >> t;
  long long p, maxy = 0, s = 0;
  vector<pair<long long, long long> > q(t);
  for (long long i = 0; i < t; i++) {
    cin >> q[i].first;
    for (long long j = 0; j < q[i].first; j++) {
      cin >> p;
      q[i].second = max(q[i].second, p);
    }
    maxy = max(q[i].second, maxy);
  }
  for (long long i = 0; i < t; i++) {
    s += (maxy - q[i].second) * q[i].first;
  }
  cout << s;
}
