#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n;
long long ans, f;
pair<long long, long long> p[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> p[i].second;
    for (int j = 0; j < p[i].second; j++) {
      long long k;
      cin >> k;
      p[i].first = max(p[i].first, k);
    }
    f = max(f, p[i].first);
  }
  for (int i = 0; i < n; i++) {
    ans += (long long)(p[i].second * (f - p[i].first));
  }
  cout << ans << endl;
  return 0;
}
