#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<int> building_size(N);
    vector<int> building_height(N);
    int max_height = -1;
    for(int i=0; i<N; ++i)
    {
        cin >> building_size[i];
        int height = -1;
        int K = building_size[i];
        for(int j=0; j<K; ++j)
        {
            int s;
            cin >> s;
            height = max(height, s);
        }
        building_height[i] = height;
        if(height>max_height)
            max_height = height;
    }
    long long ans = 0;
    for(int i=0; i<N; ++i)
    {
        ans += (long long)building_size[i] * (max_height-building_height[i]);
    }
    cout << ans << endl;
}
