#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const long long linf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double e = exp(1.0);
const double pi = acos(-1);
const double eps = 1e-6;
int a[maxn], b[maxn];
int main() {
  int n, x, f, mx = 0;
  long long ans = 0;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    b[i] = 0;
    for (int j = 1; j <= a[i]; j++) {
      scanf("%d", &x);
      b[i] = max(b[i], x);
    }
    mx = max(b[i], mx);
  }
  for (int i = 1; i <= n; i++) ans += 1LL * a[i] * (mx - b[i]);
  cout << ans << endl;
  return 0;
}
