/// Author: NemanjaSo2005
/// Date: 26th February 2023
/// Contest: Serbian high school regional contest
/// Anyone can use any part of this code for any purpose after the contest.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll N,M,mat[1505][1505],ulaz[1505][1505];
ll min(ll a,int b){
   if(a<b)
      return a;
   return b;
}
ll min(int a,ll b){
   if(a<b)
      return a;
   return b;
}
ll max(ll a,int b){
   if(a>b)
      return a;
   return b;
}
ll max(int a,ll b){
   if(a>b)
      return a;
   return b;
}
ll xoruj(ll i,ll j){
   ll ret=0;
   while(i<=N and i>=1 and j<=M and j>=1){
      ret^=mat[i][j];
      i--;
      j++;
   }
   return ret;
}
ll resi(){
  /* if(N==1){
      ll b0,b1;
      for(ll j=1;j<=M;j++)
         if(mat[1][j]==0)
            b0++;
         else
            b1++;
      return min(b0,b1);
   }*/
  /* cout<<"RESAVAM: "<<endl;
   for(int i=1;i<=N;i++){
      for(int j=1;j<=M;j++)
         cout<<mat[i][j]<<" ";
      cout<<endl;
   }*/
   ll b1=0;
   ll b0=0;
   for(ll i=1;i<N;i++)
      if(xoruj(i,1)==1)
         b1++;
      else
         b0++;
   for(ll j=1;j<=M;j++)
      if(xoruj(N,j)==1)
         b1++;
      else
         b0++;
   //cout<<"B0: "<<b0<<" B1: "<<b1<<endl;
   return min(b1,b0);
}
void solve(){
   ll res=0,MaxA=0;
   cin>>N>>M;
   for(ll i=1;i<=N;i++)
      for(ll j=1;j<=M;j++){
         cin>>ulaz[i][j];
         MaxA=max(MaxA,ulaz[i][j]);
      }
   for(ll step=1;step<=MaxA;step*=2){
      for(ll i=1;i<=N;i++)
         for(ll j=1;j<=M;j++)
            mat[i][j]=(ulaz[i][j]&step)/step;
     // cout<<resi()<<endl;
      ll dod=resi()*step;
      res+=dod;
     // cout<<step<<" "<<res<<endl;
   }
   cout<<res<<"\n";
}
void reset(){}
int main(){
   ios_base::sync_with_stdio(false);
   cin.tie(0);
   ll T=1;
 //  cin>>T;
   while(T--){
      solve();
      reset();
   }
   return 0;
}
/*
3 3
0 536870912 536870912
536870912 536870912 536870912
536870912 536870912 0
*/