#include <bits/stdc++.h>
using namespace std;
vector<int> vc[200007];
int main() {
  int n;
  scanf("%d", &n);
  int res = 0;
  for (auto i = (1); i <= (n); ++i) {
    int m;
    scanf("%d", &m);
    vc[i].resize(m);
    for (auto j = (0); j <= (m - 1); ++j) scanf("%d", &vc[i][j]);
    sort(vc[i].rbegin(), vc[i].rend());
    res = max(res, vc[i][0]);
  }
  long long ans = 0LL;
  for (auto i = (1); i <= (n); ++i)
    ans += (long long)(res - vc[i][0]) * (int)vc[i].size();
  printf("%lld\n", ans);
  return 0;
}
