#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n;
  cin >> n;
  long long numEmployees;
  deque<pair<long long, long long>> companies;
  for (int x = 0; x < n; ++x) {
    cin >> numEmployees;
    vector<long long> salaries;
    long long temp;
    long long mPT = INT_MIN;
    for (int i = 0; i < numEmployees; ++i) {
      cin >> temp;
      mPT = max(mPT, temp);
      salaries.push_back(temp);
    }
    companies.push_back(make_pair(mPT, salaries.size()));
  }
  sort(companies.begin(), companies.end());
  long long cost = 0;
  while (companies.size() > 1) {
    pair<long long, long long> val1, val2;
    val1 = companies.front();
    companies.pop_front();
    val2 = companies.front();
    companies.pop_front();
    if (val1.first == val2.first) {
      val1.second += val2.second;
      companies.push_front(val1);
    } else {
      cost += (val2.first - val1.first) * val1.second;
      val1.second += val2.second;
      val1.first = val2.first;
      companies.push_front(val1);
    }
  }
  cout << cost;
  return 0;
}
