#include <bits/stdc++.h>
using namespace std;
const long long inf = LLONG_MAX;
void solve() {
  long long n, tmp, mx = 0, ans = 0;
  cin >> n;
  vector<pair<long long, long long>> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i].second;
    v[i].first = 0;
    for (long long j = 0; j < v[i].second; j++) {
      cin >> tmp;
      v[i].first = max(tmp, v[i].first);
      mx = max(tmp, mx);
    }
  }
  for (long long i = 0; i < n; i++) {
    ans += (mx - v[i].first) * v[i].second;
  }
  cout << ans << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
