#include <bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 5;
const int inf = (int)1e9 + 7;
int n;
int mx[N], sz[N];
int main() {
  scanf("%d", &n);
  int gmax = 0;
  for (int i = 1; i <= n; ++i) {
    scanf("%d", sz + i);
    for (int j = 1; j <= sz[i]; ++j) {
      int x;
      scanf("%d", &x);
      mx[i] = max(mx[i], x);
    }
    gmax = max(gmax, mx[i]);
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += (gmax - mx[i]) * 1ll * sz[i];
  }
  printf("%lld\n", ans);
  return 0;
}
