#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > z;
inline bool comper(pair<long long, long long> a, pair<long long, long long> b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second < b.second;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int m;
    scanf("%d", &m);
    long long Max = -1;
    for (int j = 0; j < m; j++) {
      long long x;
      scanf("%I64d", &x);
      Max = max(Max, x);
    }
    z.push_back(make_pair(Max, m));
  }
  sort(z.begin(), z.end(), comper);
  long long ans = 0;
  for (int i = 0; i < (int)z.size() - 1; i++) {
    ans += ((z[i + 1].first - z[i].first) * z[i].second);
    z[i + 1].second += z[i].second;
  }
  printf("%I64d\n", ans);
  return 0;
}
