#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, j;
  cin >> n;
  long long int k = 0;
  long long int t;
  std::vector<long long int>* vec = new vector<long long int>[10000001];
  vector<long long int> vic;
  for (i = 0; i < n; i++) {
    cin >> j;
    for (k = 0; k < j; k++) {
      cin >> t;
      vec[i].push_back(t);
    }
  }
  int mymax = -999;
  long long int max = 0;
  for (i = 0; i < n; i++) {
    for (k = 0; k < vec[i].size(); k++) {
      max = vec[i][k];
      if (max > mymax) {
        mymax = max;
      }
    }
  }
  for (i = 0; i < n; i++) {
    vic.push_back(*max_element(vec[i].begin(), vec[i].end()));
  }
  long long int merge = 0;
  for (i = 0; i < n; i++) {
    if (find(vec[i].begin(), vec[i].end(), mymax) != vec[i].end()) {
      continue;
    } else {
      for (j = 0; j < vec[i].size(); j++)
        if (vec[i][j] < mymax) {
          merge += (mymax - vic[i]);
        }
    }
  }
  cout << merge;
}
