#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int q, m, l, i, s = 0;
  cin >> q;
  m = q;
  vector<pair<long long int, long long int>> b;
  while (q--) {
    long long int n, x, p = 0, z;
    cin >> n;
    z = n;
    vector<long long int> a;
    a.push_back(n);
    while (n--) {
      cin >> x;
      p = max(p, x);
    }
    b.push_back(make_pair(p, z));
  }
  sort(b.begin(), b.end());
  l = b[m - 1].first;
  for (i = 0; i < m; i++) {
    s = s + (b[i].second * (l - b[i].first));
  }
  cout << s;
}
