#include <bits/stdc++.h>
using namespace std;
long long hcf(long long a, long long b) {
  if (b == 0) return a;
  return hcf(b, a % b);
}
unsigned long long stringtoll(string s) {
  unsigned long long ans = 0;
  for (unsigned long long i = 0; i < s.length(); i++)
    ans = ans * 10 + (s[i] - '0');
  return ans;
}
vector<long long> root(long long n) {
  vector<long long> ans;
  for (long long i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      ans.push_back(i);
      if (i != n / i) ans.push_back(n / i);
    }
  }
  return ans;
}
bool ifprime(long long n) {
  for (int i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) return false;
  }
  return true;
}
long long logat(long long n, long long base) { return (log(n) / log(base)); }
long long factorial(long long n) {
  if (n == 0) return 1;
  return (n * factorial(n - 1)) % 1000000007;
}
void baspri(bool flag) {
  if (flag)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
long long lcm(long long a, long long b) { return (a / hcf(a, b)) * b; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  {
    long long n;
    cin >> n;
    vector<pair<long long, long long>> v(n);
    long long mx = 0;
    for (long long i = 0; i < n; i++) {
      cin >> v[i].first;
      v[i].second = 0;
      for (long long j = 0; j < v[i].first; j++) {
        long long temp;
        cin >> temp;
        v[i].second = max(v[i].second, temp);
      }
      mx = max(mx, v[i].second);
    }
    long long ans = 0;
    for (long long i = 0; i < n; i++) {
      ans += (mx - v[i].second) * v[i].first;
    }
    cout << ans << endl;
  }
}
