#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, i, j, k, l, m, b, t, sum, sum1, flag, max1, min1, tmp;
  cin >> t;
  vector<pair<pair<long long, long long>, long long>> v;
  j = 1;
  m = t;
  while (t--) {
    max1 = 0;
    min1 = 1e12;
    cin >> n;
    for (i = 1; i <= n; i++) {
      cin >> k;
      max1 = max(max1, k);
      min1 = min(min1, k);
    }
    v.push_back(make_pair((make_pair(max1, min1)), n));
    j++;
  }
  sort(v.begin(), v.end(),
       greater<pair<pair<long long, long long>, long long>>());
  sum = 0;
  k = v[0].first.first;
  for (i = 1; i <= m - 1; i++) {
    l = v[i].second;
    n = v[i].first.first;
    sum += (k - n) * l;
  }
  cout << sum << endl;
}
