#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 1500 + 2;
const int inf = 2e9;
int a[N][N], c[N][N];
int b[4][2][2];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            cin >> a[i][j];
        }
    }
    if (m == 1) {
        for (int i = 1; i <= n; i++) a[1][n] = a[n][1];
        swap(n, m);
    }
    if (n == 1) {
    ll ans = 0;
        for (int z = 0; z < 30; z++) {
            int sum = 0;
            for (int j = 1; j <= m; j++) if (a[1][j] & (1 << z)) sum++;
            ans += (ll) (1LL << z) * min(sum, m - sum);
        }
        cout << ans << en;
        return 0;
    }
    b[1][1][1] = b[1][1][2] = b[1][2][2] = 1;
    b[2][1][1] = b[2][2][1] = b[2][2][2] = 1;
    b[3][1][2] = b[3][2][1] = 1;
    ll ans = 0;
    for (int z = 0; z < 30; z++) {
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 2; j++) {
                if (a[i][j] & (1 << z)) c[i][j] = 1;
                else c[i][j] = 0;
            }
        }
        int mn = 4;
        for (int bb = 0; bb < 4; bb++) {
            int sum = 0;
            for (int i = 1; i <= 2; i++) {
                for (int j = 1; j <= 2; j++) {
                    sum += c[i][j] ^ b[bb][i][j];
                }
            }
            smin(mn, sum);
        }
        ans += (ll) (1LL << z) * mn;
    }
    cout << ans << en;
    return 0;
}