#include <bits/stdc++.h>
using namespace std;
const long long int MAX = 200010;
long long int n, m;
long long int x, mx;
long long int sol;
pair<long long int, long long int> p[MAX];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> m;
    mx = 0;
    for (long long int j = 0; j < m; j++) {
      cin >> x;
      mx = max(mx, x);
    }
    p[i] = {mx, m};
  }
  sort(p, p + n);
  for (long long int i = n - 2; i >= 0; i--) {
    sol += (p[i + 1].first - p[i].first) * p[i].second;
    p[i].first = p[i + 1].first;
  }
  cout << sol << "\n";
  return 0;
}
