#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  scanf("%d", &n);
  vector<pair<long long int, long long int>> a;
  long long int mx = 0;
  while (n--) {
    int m;
    scanf("%d", &m);
    long long int cur = 0;
    for (int i = 0; i < m; i++) {
      long long int temp;
      scanf("%lld", &temp);
      cur = max(cur, temp);
    }
    mx = max(mx, cur);
    a.push_back({m, cur});
  }
  long long int ans = 0ll;
  for (auto i : a) {
    ans += (0ll + (mx - i.second) * i.first * 1ll);
  }
  printf("%lld\n", ans);
  return 0;
}
