#include <bits/stdc++.h>
using namespace std;
int freq[200005];
void solve() {
  long long n, m, mx = -1, globalmx = -1;
  cin >> n;
  map<long long, long long> v;
  for (int i = 0; i < n; ++i) {
    mx = -1;
    cin >> m;
    vector<long long> vec(m);
    for (int j = 0; j < m; j++) cin >> vec[j], mx = max(mx, vec[j]);
    globalmx = max(globalmx, mx);
    if (v.find(mx) != v.end())
      v[mx] += m;
    else
      v[mx] = m;
  }
  long long cost = 0;
  for (auto c : v) {
    long long increase = globalmx - c.first;
    cost += c.second * increase;
  }
  cout << cost << "\n";
}
int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr);
  solve();
  return 0;
}
