#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void ZZ(const char* name, Arg1&& arg1) {
  std::cerr << name << " = " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void ZZ(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " = " << arg1;
  ZZ(comma, args...);
}
long long gcd(long long A, long long b) {
  if (b == 0)
    return A;
  else
    return gcd(b, A % b);
}
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<vector<long long> > sal;
  long long maxi = -1;
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    vector<long long> vec(m);
    for (int j = 0; j < m; j++) cin >> vec[j];
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    maxi = max(maxi, vec[0]);
    sal.push_back(vec);
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    long long val = (maxi - sal[i][0]);
    ans += (maxi - sal[i][0]) * sal[i].size();
  }
  cout << ans << "\n";
  return 0;
}
