#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
long long n, t, ans, maxn;
long long m[N], ms[N];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> m[i];
    for (int j = 1; j <= m[i]; j++) cin >> t, ms[i] = max(ms[i], t);
    maxn = max(maxn, ms[i]);
  }
  for (int i = 1; i <= n; i++) ans += m[i] * (maxn - ms[i]);
  cout << ans << endl;
  return 0;
}
