#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void binarni(long long x, long long maks, vector<long long>&br1, vector<long long>&br0)
{
	string s = "";
	for (int i = maks - 1; i >= 0; i--)
	{
		s += to_string((int)x / (int)pow(2, i));
		x %= (int)pow(2, i);
	}
	int j = 0;
	for (int i = s.size() - 1; i >= 0; i--)
	{
		if (s[i] == '1') br1[j++]++;
		else br0[j++]++;
	}
}
int main()
{
	int n, m; cin >> n >> m;
	vector<vector<long long>>a(n);
	long long maks = -1;
	for (int i = 0; i < n; i++)
	{
		a[i] = vector<long long>(m);
		for (int j = 0; j < m; j++)
		{
			cin >> a[i][j];
			if (ceil(log2(a[i][j])) > maks) maks = ceil(log2(a[i][j]));
		}
	}
	vector<long long>br1(maks), br0(maks);
	
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			binarni(a[i][j], maks, br1, br0);
		}
	}
	long long z = 0;
	for (int i = 0; i < maks; i++)
	{
		z += min(br1[i], br0[i]);
	}
	cout << z;
}