#include <bits/stdc++.h>
using namespace std;
int dadsadasda;
const int INF = 0x3f3f3f3f;
const long long INFLL = 1e18;
const int MOD = 1e9 + 7;
const long double PI = atan(1.0) * 4L;
const int MAXN = 1e6 + 10;
const int NN2 = (1 << 3);
int n, m, NN, num[MAXN];
vector<int> NEXT[NN2];
int dp[MAXN][NN2];
void printBin(int num) {
  for (int i = 0; i < m; i++) cout << bool(num & (1 << (m - 1 - i)));
}
int solve(int i, int mask) {
  if (i == n) return 0;
  if (dp[i][mask] != -1) return dp[i][mask];
  int &ans = dp[i][mask] = INF;
  vector<int> prox;
  if (i > 0)
    prox = NEXT[mask];
  else
    for (int i = 0; i < NN; i++) prox.push_back(i);
  for (int ne : prox) {
    int cost = 0;
    for (int j = 0; j < m; j++)
      cost += (bool(ne & (1 << j)) != bool(num[i] & (1 << j)));
    ans = min(ans, cost + solve(i + 1, ne));
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  char A[max(n, m)][min(n, m)];
  if (n > m)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> A[i][j];
  else {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) cin >> A[j][n - 1 - i];
    swap(n, m);
  }
  if (m >= 4)
    cout << -1 << endl;
  else if (m == 1)
    cout << 0 << endl;
  else {
    NN = (1 << m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (A[i][m - 1 - j] == '1') num[i] += (1 << j);
    for (int i = 0; i < NN; i++) {
      for (int j = 0; j < NN; j++) {
        bool valid = true;
        int B[2][m];
        for (int k = 0; k < m; k++) B[0][m - 1 - k] = bool(i & (1 << k));
        for (int k = 0; k < m; k++) B[1][m - 1 - k] = bool(j & (1 << k));
        for (int kj = 0; kj < m - 1; kj++) {
          int cnt = 0;
          for (int k2 = 0; k2 < 2; k2++) cnt += B[0][kj + k2];
          for (int k2 = 0; k2 < 2; k2++) cnt += B[1][kj + k2];
          valid = valid && (cnt % 2);
        }
        if (valid) NEXT[i].push_back(j);
      }
    }
    memset(dp, -1, sizeof(dp));
    int ans = solve(0, 0);
    cout << (ans >= INF ? -1 : ans) << endl;
  }
  return 0;
}
