#include <bits/stdc++.h>
using namespace std;
bool cmp(pair<int, int> &a, pair<int, int> &b) {
  if (a.first == b.first) {
    a.second > b.second;
  }
  return a.first > b.first;
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > v;
  for (int i = 0; i < n; i++) {
    int m, temp, maxi = 0;
    cin >> m;
    for (int j = 0; j < m; j++) {
      cin >> temp;
      maxi = max(maxi, temp);
    }
    v.push_back({maxi, m});
  }
  sort(v.begin(), v.end(), cmp);
  long long ans = 0;
  for (int i = 1; i < v.size(); i++) {
    ans += (long long)(v[0].first - v[i].first) * (long long)v[i].second;
  }
  cout << ans << endl;
}
