#include <bits/stdc++.h>
#define FAST {ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define ll long long
#define ull unsigned long long
#define pqueue priority_queue
#define pb push_back
#define fi first
#define se second
#define ld long double
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(a) (a).begin(), (a).end()
#define mp make_pair
using namespace std;
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<pll, null_type,less<pll>, rb_tree_tag,tree_order_statistics_node_update>
*/
// order_of_key -> # less than k
// find_by_order -> k-th element
// pq max element

const int NMAX=1501;

ll dp[NMAX][NMAX][2][31];
bool memo[NMAX][NMAX][2][31];
ll mat[NMAX][NMAX];
int n,m;

ll delta(int i, int j, int p, int it) {
    //cout << i << ' ' << j << ' ' << p << ' ' << it << endl;
    if ( memo[i][j][p][it] ) {
        return dp[i][j][p][it];
    }
    if ( i == n-1 && j == m-1 ) {
        if (p==0) return 0;
        dp[i][j][p][it] = (mat[i][j]^(1<<it))-mat[i][j];
        return (mat[i][j]^(1<<it))-mat[i][j];
    }
    ll res = 0;
    if ( p == 0 ) {
        ll res1 = 0;
        ll res2 = LLONG_MAX/4;
        int ok = 0;
        if (i<n-1) {
            res1+=delta(i+1,j,0,it);
            res2+=delta(i+1,j,1,it);
            ok++;
        }
        if (j<m-1) {
            res1+=delta(i,j+1,0,it);
            res2+=delta(i,j+1,1,it);
            ok++;
        }
        if (ok==2)
        res = min(res1, res2);
        else res = res1;

        res = min(res,0LL);
        memo[i][j][p][it] = 1;
        dp[i][j][p][it] = res;
        return res;
    }
    res =(mat[i][j]^(1<<it))-mat[i][j];

    ll d1p = 0;
    ll d1n = LLONG_MAX/4;
    ll d2p = 0;
    ll d2n = LLONG_MAX/4;
    if (i<n-1) {
        d1p=delta(i+1,j,0,it);
        d1n=delta(i+1,j,1,it);
    }
    if (j<m-1) {
        d2p=delta(i,j+1,0,it);
        d2n=delta(i,j+1,1,it);
    }
    memo[i][j][p][it] = 1;
    res += min(d1p+d2n, d1n+d2p);
    dp[i][j][p][it] = res;
    return res;

}

int main () {
    FAST
    cin >> n >> m ;
    ll s = 0;
    for (int i = 0 ; i < n ; i++ ) {
        for (int j = 0; j < m ; j++ ){
            cin >> mat[i][j];
            s+= mat[i][j];
        }
    }



    for (int i = 0 ; i < 31; i++ ){
        //cout << s << endl;
        s += min(delta(0,0,0,i),delta(0,0,1,i));

    }
    /*
    for (int i= 0 ; i< n; i++ ) {
        for (int j = 0 ; j < m ; j++ ) {
            cout << dp[i][j][1][0] << ' ';
        }
        cout << endl;
    }
    //*/

    cout << s << endl;



}






/*

2 2
1 1
0 1


2 3
0 1 1
1 1 0

//*/