#include <bits/stdc++.h>
using namespace std;
int main() {
  int Size;
  cin >> Size;
  int Biggest = 0;
  unsigned long long int **Company = new unsigned long long int *[Size];
  for (int i = 0; i < Size; i++) {
    Company[i] = new unsigned long long int[2];
  }
  for (int i = 0; i < Size; i++) {
    int n;
    cin >> n;
    unsigned long long int X;
    unsigned long long int Biggest = 0;
    for (int j = 0; j < n; j++) {
      cin >> X;
      if (X > Biggest) Biggest = X;
    }
    Company[i][0] = n;
    Company[i][1] = Biggest;
  }
  int Index = 0;
  Biggest = 0;
  for (int i = 0; i < Size; i++) {
    if (Company[i][1] > Biggest) {
      Index = i;
      Biggest = Company[i][1];
    }
  }
  unsigned long long int Counter = 0;
  for (int i = 0; i < Size; i++) {
    if (i != Index) {
      Counter += (Biggest - Company[i][1]) * Company[i][0];
    }
  }
  cout << Counter;
  return 0;
}
