#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 200000;
int n;
vector<int> sal[MAXN];
int mxsal[MAXN];
long long solve() {
  for (int i = (0); i < (n); ++i) {
    mxsal[i] = sal[i][0];
    for (int j = (1); j < (((int)(sal[i]).size())); ++j)
      mxsal[i] = max(mxsal[i], sal[i][j]);
  }
  int globmx = mxsal[0];
  for (int i = (1); i < (n); ++i) globmx = max(globmx, mxsal[i]);
  long long ret = 0;
  for (int i = (0); i < (n); ++i)
    ret += (long long)(globmx - mxsal[i]) * ((int)(sal[i]).size());
  return ret;
}
void run() {
  scanf("%d", &n);
  for (int i = (0); i < (n); ++i) {
    int cnt;
    scanf("%d", &cnt);
    sal[i] = vector<int>(cnt);
    for (int j = (0); j < (cnt); ++j) scanf("%d", &sal[i][j]);
  }
  printf("%lld\n", solve());
}
int main() {
  run();
  return 0;
}
