#include <bits/stdc++.h>
using namespace std;
double PI = 3.141592653589793;
const double EPS = 1e-8;
const int N = 1e6 + 5;
const long long mod = 1e9 + 7;
const int oo = 1e9;
int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};
int dy[] = {1, -1, 0, 0, -1, 1, 1, -1};
int n, m;
pair<long long, long long> t[N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i <= n - 1; i++) {
    scanf("%d", &m);
    t[i].second = m;
    long long mx = -oo;
    for (int j = 0; j <= m - 1; j++) {
      long long a;
      scanf("%lld", &a);
      mx = max(mx, a);
    }
    t[i].first = mx;
  }
  sort(t, t + n);
  long long ans = 0;
  for (int i = 0; i <= n - 1; i++) {
    ans += (t[n - 1].first - t[i].first) * t[i].second;
  }
  printf("%lld", ans);
}
