#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 20;
long long n, a[N], b[N], d = 0, c = 0;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long m;
    cin >> m;
    for (int j = 0; j < m; j++) {
      long long x;
      cin >> x;
      b[i] = b[i] + 1;
      a[i] = max(a[i], x);
    }
  }
  for (int i = 0; i < n; i++) {
    d = max(a[i], d);
  }
  for (int i = 0; i < n; i++) {
    c = c + ((d - a[i]) * b[i]);
  }
  cout << c;
  return 0;
}
