#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const long long MOD = 998244353;
std::vector<long long> v[200005];
int main(int argc, char const *argv[]) {
  long long n;
  cin >> n;
  for (int i = 0; i <= n - 1; ++i) {
    long long a;
    cin >> a;
    while (a--) {
      long long t;
      cin >> t;
      v[i].push_back(t);
    }
    sort(v[i].rbegin(), v[i].rend());
  }
  long long ans = 0, max = v[0][0], siz = v[0].size();
  for (int i = 1; i <= n - 1; ++i) {
    long long toincrease = abs(v[i][0] - max);
    if (v[i][0] >= max) {
      ans += toincrease * siz;
      siz += v[i].size();
      max = v[i][0];
    } else {
      ans += toincrease * v[i].size();
      siz += v[i].size();
    }
  }
  cout << ans;
}
