#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char** argv) {
	ios_base::sync_with_stdio(false);
	
	unsigned long long int n,m;
	cin >>n>>m;
	unsigned long long int bin[32];
	if(n==1 || m==1)
	{
	
	unsigned long long int brojac[32];
	bin[0]=1;
	brojac[0]=0;
	for(int i=1;i<32;i++)
	{
		brojac[i]=0;
		bin[i]=bin[i-1]*2;
	}
	
	for(int i=0;i<m*n;i++)
	{
		unsigned long long int x;
		cin >>x;
		for(int i=0;i<32;i++)
		{
			if(x&bin[i])
			{
				brojac[i]++;
			}
		}
	}
	
	unsigned long long int h=n+m-1;
/*	for(int i=0;i<32;i++)
	{
		cout <<brojac[i]<<" ";
	}
	cout <<"\n";*/
   
	for(int i=0;i<32;i++)
	{
		brojac[i]=brojac[i]%h;
		brojac[i]=min(brojac[i],h-brojac[i]);
	}
    
	/*for(int i=0;i<32;i++)
	{
		cout <<brojac[i]<<" ";
	}
	cout <<"\n";*/
	unsigned long long int z=0;
	for(int i=0;i<32;i++)
	{
		z+=brojac[i]*bin[i];
		//cout <<z<<" ";
	}
	//cout <<"\n";
	cout <<z<<"\n";
	}
	else
	{
		bin[0]=1;
	for(int i=1;i<32;i++)
	{
		bin[i]=bin[i-1]*2;
	}
	
	int brojevi[n][m];
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin >>brojevi[i][j];
		}
	}
	
	unsigned long long int z=0;
	for(int i=0;i<32;i++)
	{
		unsigned long long int a=brojevi[0][0]&bin[i];
		unsigned long long int b=brojevi[0][1]&bin[i];
		unsigned long long int c=brojevi[1][0]&bin[i];
		unsigned long long int d=brojevi[1][1]&bin[i];
		unsigned long long int x=bin[i];
		//cout <<a<<" "<<b<<" "<<c<<" "<<d<<"\n";
		//cout <<(a^x)<<" "<<(b^x)<<" "<<(c^x)<<" "<<(d^x)<<"\n";
		z+=min(min(a+b+c+d,(a^x)+(b^x)+c+(d^x)),min((a^x)+b+(c^x)+(d^x),a+(b^x)+(c^x)+d));
	}
	cout <<z<<"\n";
	
	}
	return 0;
}