#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
long long company[maxn];
bool cmp(long long a, long long b) { return a > b; }
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int m;
  cin >> m;
  long long ans = 0, max1, max2;
  for (int i = 0; i < m; ++i) cin >> company[i];
  sort(company, company + m, cmp);
  max1 = company[0];
  for (int i = 1; i < n; ++i) {
    int m1;
    cin >> m1;
    for (int i = 0; i < m1; ++i) cin >> company[i];
    sort(company, company + m1, cmp);
    max2 = company[0];
    if (max1 > max2) {
      ans += (max1 - max2) * m1;
      max1 = max1;
      m = m + m1;
    } else {
      ans += (max2 - max1) * m;
      max1 = max2;
      m = m1 + m;
    }
  }
  cout << ans << endl;
  return 0;
}
