#include <bits/stdc++.h>
using namespace std;
int n;
pair<long long, long long> a[200001];
unsigned long long ans;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i].second;
    for (int j = 1; j <= a[i].second; ++j) {
      long long b;
      cin >> b;
      a[i].first = max(a[i].first, b);
    }
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i < n; ++i) {
    ans += (a[i + 1].first - a[i].first) * a[i].second;
    a[i + 1].second += a[i].second;
  }
  cout << ans;
}
