#include <bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define FOD(i,a,b) for(int i=a; i>=b; i--)
#define pa pair<int,int>
#define fi first
#define se second
#define MOD xxx
#define getbit(n,i) ((n>>(i-1))&1)
#define offbit(n,i) ((n^(1<<(i-1))
#define onbit(n,i) (n|(1<<(i-1)))
#define cntone(x) (__builtin_popcount(x))
using namespace std;
const int N = 80+5;
const int P = 12800;
int m,n;
int A[N][N],B[N][N];
int F[N][N][N*N*4];
int tmp,ans(0);
int main()
{
    cin >> m >> n;
    FOR(i,1,m)
    FOR(j,1,n) cin >> A[i][j];
    FOR(i,1,m)
    FOR(j,1,n) cin >> B[i][j];
    tmp = A[1][1] - B[1][1];
    F[1][1][tmp +P] = F[1][1][-tmp +P] = 1;
    FOR(i,1,m)
    FOR(j,1,n)
    if(i+j!=2)
    {
        tmp = A[i][j] - B[i][j];
        FOR(k,0,2*P)
        if(F[i-1][j][k] || F[i][j-1][k])
        {
            F[i][j][k+tmp] = F[i][j][k-tmp] = 1;
        }
    }
    FOR(i,0,P)
    {
        if(F[m][n][i+P]==1)
        {
            ans = i;
            break;
        }
        if(F[m][n][i])
        ans = i-P;
    }
    cout << abs(ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

long long n, x, cur = 0;
vector<vector<long long>> tests;

long long check(long long k){

    long long best, complete = k / x, other = k % x, cursum = 0;

    if(complete >= n)
        return cur + 1;

    for(int i = 0; i < complete; ++i)
        cursum += tests[i][0];

    best = cursum;

    for(int i = 0; i < n; ++i){
        if(i < complete){
            best = max(best, tests[complete][0] + (cursum - tests[i][0]) + tests[i][3] * other - tests[i][1] * (tests[i][3] - tests[i][2]));
            best = max(best, tests[complete][0] + (cursum - tests[i][0]) + tests[i][2] * other);
        } else {
            best = max(best, cursum + tests[i][3] * other - tests[i][1] * (tests[i][3] - tests[i][2]));
            best = max(best, cursum + tests[i][2] * other);
        }
    }
    return best;
}

int main(){

    cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);

    long long b, l ,u; cin >> n >> x;

    for(int i = 0; i < n; ++i){
        cin >> b >> l >> u;
        cur += b * l;
        tests.push_back({(x - b) * u + b * l, b, l, u});
    }

    sort(tests.begin(), tests.end(), greater<vector<long long>>());

    long long mid, lef = -1, rig = n * x;
    for(;lef < rig - 1;){
        mid = lef + rig >> 1;
        if(check(mid) >= cur)
            rig = mid;
        else
            lef = mid;
    }
    cout << rig << "\n";
    return 0;
}