#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n, mx[maxn], sz[maxn], mx_tot = -1;
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int tmp = -1;
    scanf("%d", &sz[i]);
    for (int j = 0; j < sz[i]; j++) {
      int x;
      scanf("%d", &x);
      if (x > tmp) tmp = x;
    }
    mx[i] = tmp;
    if (mx[i] > mx_tot) mx_tot = mx[i];
  }
  long long sum = 0;
  for (int i = 0; i < n; i++) {
    sum += (long long)sz[i] * (long long)(mx_tot - mx[i]);
  }
  cout << sum << '\n';
  return 0;
}
