#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<long long, long long>> v(n);
  for (int i = 0; i < n; i++) {
    long long m;
    cin >> m;
    int l = -1;
    for (int j = 0; j < m; j++) {
      int a;
      cin >> a;
      l = max(l, a);
    }
    v[i] = {l, m};
  }
  long long c = 0;
  pair<long long, long long> p = v[0];
  for (int i = 1; i < n; i++) {
    if (p.first >= v[i].first) {
      c += (long long)(p.first - v[i].first) * (long long)(v[i].second);
      p = {p.first, p.second + v[i].second};
    } else {
      c += (long long)abs(p.first - v[i].first) * (long long)(p.second);
      p = {v[i].first, p.second + v[i].second};
    }
  }
  cout << c;
}
