#include <bits/stdc++.h>
using namespace std;
long long Calculation(int y, int m, int d) {
  if (m < 3) {
    y -= 1;
    m += 12;
  }
  return 365 * y + y / 4 - y / 100 + y / 400 + (153 * m - 457) / 5 + d - 306;
}
bool cmp(int a, int b) { return a > b; }
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline string r(string n) {
  string k = "";
  for (int i = n.size() - 1; i >= 0; i--) k += n[i];
  return k;
}
const long long modd = 1e9 + 7;
long long quickpow(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) {
      ret = ret * a % modd;
    }
    a = a * a % modd;
    b >>= 1;
  }
  return ret;
}
int main() {
  long long i, j, k, n1, m[200005], n[200005];
  cin >> k;
  long long maxx = 0;
  for (i = 0; i < k; i++) {
    long long t;
    cin >> t;
    long long max1 = 0;
    for (j = 0; j < t; j++) {
      long long x;
      cin >> x;
      max1 = max(x, max1);
    }
    n[i] = max1;
    m[i] = t;
    maxx = max(maxx, max1);
  }
  long long ans = 0;
  for (i = 0; i < k; i++) {
    ans += ((maxx - n[i]) * m[i]);
  }
  cout << ans << endl;
}
