#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5, MN = 1e9;
long long n, a, mx, mxx, m, cnt;
vector<long long> v1, v2;
int main() {
  cin >> n;
  for (long long i = 0; i < n; i++) {
    mx = 0;
    cin >> m;
    for (long long i = 0; i < m; i++) {
      cin >> a;
      if (a > mxx) {
        mxx = a;
      }
      if (a > mx) {
        mx = a;
      }
    }
    v2.push_back(m);
    v1.push_back(mx);
  }
  for (long long i = 0; i < v1.size(); i++) {
    if (v1[i] != mxx) {
      cnt += (mxx - v1[i]) * v2[i];
    }
  }
  cout << cnt;
  cout << "\n";
  return 0;
}
