#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10, mod = 1e9 + 7;
const long long inf = 1e18;
pair<int, int> p[maxn];
int mm;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, mx = 0;
    cin >> x;
    for (int j = 0; j < x; j++) {
      int y;
      cin >> y;
      mx = max(mx, y);
      mm = max(mm, y);
    }
    p[i] = {x, mx};
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    ans += 1ll * p[i].first * (mm - p[i].second);
  }
  cout << ans << endl;
  return 0;
}
