#include <bits/stdc++.h>
using namespace std;
const int inf = 1 << 30;
const int maxn = 2e5 + 5;
int mx[maxn], si[maxn], n;
int read() {
  int x = 0, f = 1;
  char ch;
  for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())
    if (ch == '-') f = -f;
  for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
  return x * f;
}
int main() {
  n = read();
  int fin = 0;
  for (int i = 1; i <= n; i++) {
    si[i] = read();
    mx[i] = 0;
    for (int j = 1; j <= si[i]; j++) {
      int px;
      px = read();
      mx[i] = max(mx[i], px);
      fin = max(fin, px);
    }
  }
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int delta = fin - mx[i];
    ans += 1ll * delta * si[i];
  }
  printf("%I64d\n", ans);
  return 0;
}
