#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<long long> v(n), w(n);
  long long mx = 0;
  for (long long i = 0; i < n; i++) {
    long long x = 0;
    long long m;
    cin >> m;
    w[i] = m;
    while (m--) {
      long long y;
      cin >> y;
      x = max(x, y);
    }
    v[i] = x;
    mx = max(mx, x);
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans += (mx - v[i]) * w[i];
  }
  cout << ans << "\n";
  return 0;
}
