#include <bits/stdc++.h>
using namespace std;
vector<int> vec[200005];
long long a[200005];
long long b[200005];
int main() {
  int n;
  cin >> n;
  long long maxn = -1;
  for (int i = 1; i <= n; i++) {
    int k;
    cin >> k;
    a[i] = k;
    for (int j = 1; j <= k; j++) {
      long long m;
      cin >> m;
      maxn = max(maxn, m);
      b[i] = max(b[i], m);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += (maxn - b[i]) * a[i];
  }
  cout << ans << "\n";
}
