#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  unsigned long long n;
  cin >> n;
  vector<pair<unsigned long long, unsigned long long>> v(n);
  unsigned long long maxmax = 0;
  for (int i = 0; i < n; i++) {
    unsigned long long m, a;
    cin >> m;
    unsigned long long mx = 0;
    for (int j = 0; j < m; j++) {
      cin >> a;
      mx = max(a, mx);
    }
    maxmax = max(mx, maxmax);
    v[i] = {mx, m};
  }
  unsigned long long ans = 0;
  for (pair<unsigned long long, unsigned long long> p : v) {
    ans += (maxmax - p.first) * p.second;
  }
  cout << ans;
  return 0;
}
