#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
const int INF = (long long)1e9 + 5;
int loc[MAXN];
vector<int> v[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int N, mx = 0;
  cin >> N;
  for (int i = 0; i < N; i++) {
    int m;
    cin >> m;
    loc[i] = 0;
    for (int j = 0; j < m; j++) {
      int x;
      cin >> x;
      v[i].push_back(x);
      loc[i] = max(loc[i], x);
    }
    mx = max(mx, loc[i]);
  }
  long long ans = 0;
  for (int i = 0; i < N; i++) {
    ans += (long long)(mx - loc[i]) * (long long)v[i].size();
  }
  cout << ans << "\n";
}
