#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
multiset<pair<int, int> > s;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    int maxi = 0;
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      maxi = max(maxi, a);
    }
    s.insert({maxi, m});
  }
  long long ans = 0;
  while (s.size() > 1) {
    pair<int, int> k = (*s.begin());
    s.erase(s.find(k));
    pair<int, int> p = (*s.begin());
    s.erase(s.find(p));
    if (p.first == k.first) {
      s.insert({p.first, p.second + k.second});
    } else {
      ans += 1LL * (p.first - k.first) * k.second;
      s.insert({p.first, p.second + k.second});
    }
  }
  cout << ans << "\n";
}
