#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  long long n, inp;
  cin >> n;
  std::vector<long long> mx(n, 0), m(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> m[i];
    for (int j = 0; j < m[i]; j++) {
      cin >> inp;
      if (inp > mx[i]) mx[i] = inp;
    }
  }
  long long MX = 0;
  for (int i = 0; i < n; i++)
    if (mx[i] > MX) MX = mx[i];
  long long ans = 0;
  for (int i = 0; i < n; i++) ans += (MX - mx[i]) * m[i];
  cout << ans << "\n";
  return 0;
}
