#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<pair<int, int>> b;
  for (int i = 0; i < n; ++i) {
    int m;
    cin >> m;
    int big = 0;
    for (int j = 0; j < m; ++j) {
      int a;
      cin >> a;
      big = max(a, big);
    }
    b.push_back({big, m});
  }
  sort(b.begin(), b.end());
  ll ans = 0;
  int cur = 0;
  for (int i = 1; i < n; ++i) {
    cur += b[i - 1].second;
    ans += 1LL * cur * (b[i].first - b[i - 1].first);
  }
  cout << ans;
  return 0;
}
