#include <bits/stdc++.h>
using namespace std;
const long long mxN = 2e5 + 5;
const long long INF = 1e18 + 7;
void _print(long long t) { cerr << t; }
void _print(string t) { cerr << t; }
void _print(bool t) { cerr << t; }
void _print(char t) { cerr << t; }
void _print(long double t) { cerr << t; }
void _print(double t) { cerr << t; }
void _print(unsigned long long t) { cerr << t; }
template <class T>
void _print(T arr[], long long i, long long j) {
  cerr << "[ ";
  for (long long k = i; k <= j; k++) cerr << arr[k] << ", ";
  cerr << "]\n";
}
template <class T, class V>
void _print(pair<T, V> p);
template <class T>
void _print(vector<T> v);
template <class T>
void _print(set<T> v);
template <class T, class V>
void _print(map<T, V> v);
template <class T>
void _print(multiset<T> v);
template <class T, class V>
void _print(pair<T, V> p) {
  cerr << "{";
  _print(p.ff);
  cerr << ",";
  _print(p.ss);
  cerr << "}";
}
template <class T>
void _print(vector<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(set<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T>
void _print(multiset<T> v) {
  cerr << "[ ";
  for (T i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
template <class T, class V>
void _print(map<T, V> v) {
  cerr << "[ ";
  for (auto i : v) {
    _print(i);
    cerr << " ";
  }
  cerr << "]";
}
long long N, M, X;
vector<long long> A[mxN];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N;
  long long maxx = -INF;
  for (long long i = 1; i <= N; i++) {
    cin >> M;
    for (long long j = 1; j <= M; j++) {
      cin >> X;
      A[i].push_back(X);
      maxx = max(maxx, X);
    }
  }
  long long ans = 0;
  for (long long i = 1; i <= N; i++) {
    long long maxHere = -INF;
    for (long long j = 0; j < A[i].size(); j++) maxHere = max(maxHere, A[i][j]);
    ans += (maxx - maxHere) * (long long)A[i].size();
  }
  cout << ans;
  return 0;
}
