#include <bits/stdc++.h>
using namespace std;
vector<long long> a[333333];
long long b[333333];
int main() {
  long long n, maxx = 0;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long m, x;
    cin >> m;
    while (m--) {
      cin >> x;
      a[i].push_back(x);
      b[i] = max(x, b[i]);
    }
    maxx = max(maxx, b[i]);
  }
  long long sum = 0;
  for (long long i = 1; i <= n; i++) {
    sum += a[i].size() * (maxx - b[i]);
  }
  cout << sum;
  return 0;
}
