#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, b, j, k, i, s = 0, maxi = -1, c, d;
  cin >> a;
  long long int ara[a], ara1[a];
  vector<long long int> v;
  vector<long long int> v1;
  for (i = 0; i < a; i++) {
    cin >> b;
    ara[i] = b;
    for (j = 0; j < b; j++) {
      cin >> c;
      v.push_back(c);
    }
    sort(v.begin(), v.begin() + b);
    v1.push_back(v[b - 1]);
    ara1[i] = v[b - 1];
    v.erase(v.begin(), v.end());
  }
  sort(v1.begin(), v1.end());
  maxi = v1[a - 1];
  for (i = 0; i < a; i++) {
    s = s + ((maxi - ara1[i]) * ara[i]);
  }
  cout << s << endl;
  return 0;
}
