#include <bits/stdc++.h>
using namespace std;
const int N = 3e5, Mod = 998244353, INF = 1e9 + 7;
pair<long long, long long> p[N];
void GetAns() {
  long long n = 0, ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    long long m = 0, k = 0;
    cin >> m;
    for (int j = 1; j <= m; j++) {
      long long a = 0;
      cin >> a;
      k = max(k, a);
    }
    p[i].first = k;
    p[i].second = m;
  }
  sort(p + 1, p + n + 1);
  for (int i = 1; i < n; i++) {
    ans += (p[i + 1].first - p[i].first) * p[i].second;
    p[i + 1].second += p[i].second;
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    GetAns();
  }
}
