#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<long long> a[n];
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    vector<long long> v(m);
    for (int j = 0; j < m; j++) cin >> v[j];
    sort(v.begin(), v.end());
    reverse(v.begin(), v.end());
    for (int j = 0; j < m; j++) a[i].push_back(v[j]);
  }
  long long maxe = INT_MIN;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    maxe = max(maxe, a[i][0]);
  }
  for (int i = 0; i < n; i++) {
    ans += (maxe - a[i][0]) * ((long long)(a[i].size()));
  }
  cout << ans;
  return 0;
}
