#include <bits/stdc++.h>
using namespace std;
struct comp {
  long long cnt, mmax;
} com[200010];
long long cmp(comp a, comp b) { return a.mmax < b.mmax; }
signed main() {
  long long n, k, inp1, inp2, immax = 0, sum = 0, mmax = 0;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> inp1;
    com[i].cnt = inp1;
    immax = 0;
    for (long long j = 0; j < inp1; j++) {
      cin >> inp2;
      immax = max(immax, inp2);
    }
    com[i].mmax = immax;
    mmax = max(immax, mmax);
  }
  sort(com, com + n, cmp);
  for (long long i = 0; i < n; i++) {
    if (com[i].mmax == mmax) break;
    sum += (mmax - com[i].mmax) * com[i].cnt;
  }
  cout << sum << endl;
  return 0;
}
