#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long m, temp, mxSal;
  vector<pair<long long, long long> > vec;
  long long res = 0;
  for (int i = 0; i < n; i++) {
    cin >> m;
    mxSal = 0;
    for (int j = 0; j < m; j++) {
      cin >> temp;
      mxSal = max(mxSal, temp);
    }
    vec.push_back(pair<long long, long long>(mxSal, m));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < vec.size() - 1; i++) {
    res += (vec[i].second * (vec[n - 1].first - vec[i].first));
  }
  cout << res << endl;
}
