#include <bits/stdc++.h>
using namespace std;
long long n, i, m[1000000], mx[1000000], mxx, l, a, ans;
int main() {
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> m[i];
    for (l = 1; l <= m[i]; l++) {
      cin >> a;
      mx[i] = max(a, mx[i]);
    }
  }
  for (i = 1; i <= n; i++) {
    mxx = max(mxx, mx[i]);
  }
  for (i = 1; i <= n; i++) {
    ans += (mxx - mx[i]) * m[i];
  }
  cout << ans;
}
