#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
template <typename T>
void print(T v) {
  for (auto i = v.begin(); i != v.end(); i++) cout << *i << " ";
  cout << endl;
}
template <typename T>
void print_vv(T v, bool same_line = true) {
  for (auto i = 0; i < v.size(); i++) {
    cout << "{";
    for (auto j = 0; j != v[i].size(); j++) {
      cout << setw(3) << v[i][j] << ",";
    }
    cout << "},";
    if (same_line) cout << endl;
  }
  cout << endl;
}
int main() {
  int n, m, sal;
  cin >> n;
  long long sz[n];
  long long mx[n];
  for (int i = 0; i < n; i++) {
    cin >> m;
    sz[i] = m;
    int m_sal = INT_MIN;
    for (int j = 0; j < m; j++) {
      cin >> sal;
      m_sal = max(m_sal, sal);
    }
    mx[i] = m_sal;
  }
  long long inc = 0;
  long long prev_m_sal = mx[0];
  long long prev_sz = sz[0];
  for (int i = 1; i < n; i++) {
    if (prev_m_sal < mx[i]) {
      inc += (mx[i] - prev_m_sal) * prev_sz;
    } else if (prev_m_sal > mx[i]) {
      inc += (prev_m_sal - mx[i]) * sz[i];
    }
    prev_sz += sz[i];
    prev_m_sal = max(prev_m_sal, mx[i]);
  }
  cout << inc << endl;
  return 0;
}
