#include <bits/stdc++.h>
using namespace std;
long long n, b[200001], a[2000001], mx2, mx[200001], ans;
int main() {
  cin >> n;
  mx2 = -1;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    for (int j = 1; j <= b[i]; ++j) {
      cin >> a[j];
      if (j == 1) {
        mx[i] = a[j];
      }
      mx[i] = max(mx[i], a[j]);
    }
    mx2 = max(mx2, mx[i]);
  }
  for (int i = 1; i <= n; ++i) {
    ans += (mx2 - mx[i]) * b[i];
  }
  cout << ans;
  return 0;
}
