#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 5;
struct Node {
  long long x, y;
} a[MAXN];
int main() {
  long long n;
  cin >> n;
  long long maxn = 0;
  for (long long i = 0; i < n; i++) {
    cin >> a[i].x;
    long long x;
    for (long long j = 0; j < a[i].x; j++) {
      cin >> x;
      maxn = max(maxn, x);
      a[i].y = max(a[i].y, x);
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    ans = ans + a[i].x * (maxn - a[i].y);
  }
  cout << ans << endl;
}
