#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
template <class T>
inline void ckmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void ckmax(T &a, T b) {
  if (b > a) a = b;
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
int main() {
  std::ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<long long> c(n), s(n), w(n);
  for (int i = 0; i < (n); ++i) {
    cin >> c[i];
    s[i] = w[i] = 0;
    for (int j = 0; j < (c[i]); ++j) {
      int p;
      cin >> p;
      s[i] += p;
      ckmax(w[i], (long long)p);
    }
  }
  long long e = 0;
  for (int i = 0; i < (n); ++i) ckmax(e, w[i]);
  long long ret = 0;
  for (int i = 0; i < (n); ++i) ret += c[i] * (e - w[i]);
  cout << ret << endl;
  return 0;
}
