#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long i, k, j, l, sum = 0, n, m, max;
  cin >> n;
  vector<pair<long long, long long> > v;
  for (i = 0; i < (n); i++) {
    cin >> m;
    cin >> j;
    max = j;
    for (k = 1; k < (m); k++) {
      cin >> j;
      if (max < j) max = j;
    }
    v.push_back(make_pair(max, m));
  }
  sort(v.begin(), v.end());
  max = v[n - 1].first;
  for (i = 0; i < (n); i++) sum = sum + (max - v[i].first) * v[i].second;
  cout << sum;
}
