#include <bits/stdc++.h>
using namespace std;
int n, a, mk = INT_MIN, mki = INT_MIN, tut[200005], ar[200005];
vector<int> v[200005];
long long sum;
int main() {
  cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> ar[i];
    a = ar[i];
    int tmp;
    for (int j = 1; j <= a; j++) cin >> tmp, mki = max(mki, tmp);
    tut[i] = mki;
    mk = max(mki, mk);
    mki = INT_MIN;
  }
  for (int i = 1; i <= n; i++) {
    long long int tut1 = mk - tut[i];
    sum += tut1 * ar[i];
  }
  cout << sum;
}
