#include <bits/stdc++.h>
using namespace std;
const int mxn = 2e5 + 1;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    int n, i;
    long long ans = 0, mx = 0, j, m;
    cin >> n;
    vector<vector<long long>> loser;
    for (i = 0; i < n; i++) {
      cin >> m;
      vector<long long> himj;
      for (j = 0; j < m; j++) {
        long long x;
        cin >> x;
        himj.push_back(x);
        mx = max(mx, x);
      }
      sort(himj.begin(), himj.end());
      reverse(himj.begin(), himj.end());
      loser.push_back(himj);
    }
    for (i = 0; i < n; i++) {
      ans += loser[i].size() * (mx - loser[i][0]);
    }
    cout << ans;
  }
  return 0;
}
