#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  vector<pair<int, int> > ip;
  cin >> n;
  while (n--) {
    int m;
    cin >> m;
    int mx = 0;
    int cnt = m;
    while (m--) {
      int x;
      cin >> x;
      mx = max(mx, x);
    }
    ip.push_back({mx, cnt});
  }
  sort(ip.begin(), ip.end());
  long long ans = 0;
  for (int i = ip.size() - 2; i >= 0; --i) {
    ans += (ip[ip.size() - 1].first - ip[i].first) * 1LL * ip[i].second;
  }
  cout << ans;
}
