#include <bits/stdc++.h>
using namespace std;
inline void solve() {
  long long int n;
  cin >> n;
  pair<long long int, long long int> val[n];
  long long int totmax = -1;
  for (long long int i = 0; i < n; i++) {
    long long int a;
    cin >> a;
    long long int mx = -1;
    for (long long int j = 0; j < a; j++) {
      long long int x;
      cin >> x;
      mx = max(mx, x);
    }
    val[i] = {a, mx};
    totmax = max(totmax, mx);
  }
  long long int ans = 0;
  for (long long int i = 0; i < n; i++) {
    ans += (val[i].first * (totmax - val[i].second));
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tests = 1;
  while (tests--) {
    solve();
  }
}
