#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long N = 1e5 + 1;
const long long mod = 1e9 + 7;
const long double eps = 1E-7;
long long n, m, a, b, x, c, d, ans;
string s1, s2;
vector<long long> V, F;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> m;
    for (int j = 1; j <= m; j++) {
      cin >> x;
      b = max(b, x);
    }
    if (i == 1) {
      d = b;
      c = m;
    } else {
      if (b > d) {
        ans += (abs(b - d) * c);
        c = c + m;
        d = max(b, d);
      } else {
        ans += (abs(b - d) * m);
        c = c + m;
        d = max(b, d);
      }
    }
    b = 0;
  }
  cout << ans << endl;
}
