#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long plati = 0;
  cin >> n;
  int ***a = new int **[n];
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    int maks = 0;
    a[i] = new int *[2];
    a[i][0] = new int(m);
    for (int j = 0; j < m; j++) {
      int r;
      cin >> r;
      if (r > maks) maks = r;
    }
    a[i][1] = new int(maks);
  }
  int najs = 0;
  for (int i = 0; i < n; i++) {
    if (*a[i][1] > najs) najs = *a[i][1];
  }
  for (int i = 0; i < n; i++) {
    plati += (long long)*a[i][0] * (najs - *a[i][1]);
  }
  cout << plati;
  for (int i = 0; i < n; i++) {
    delete a[i][0];
    delete a[i][1];
    delete[] a[i];
  }
  delete[] a;
  return 0;
}
