#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
string s[1000005];
int n, m;
int g[1000005][5];
int p[1000005];
int cur[3][5];
int dp[1000005][8];
vector<int> v;
vector<int> ans;
bool valid(int last, int nxt) {
  for (int i = 0; i < m; i++) cur[0][i] = (last & (1 << i)) ? 1 : 0;
  for (int i = 0; i < m; i++) cur[1][i] = (nxt & (1 << i)) ? 1 : 0;
  int n = 2, f = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i + 2 > n or j + 2 > m) continue;
      f &=
          ((cur[i][j] + cur[i + 1][j] + cur[i][j + 1] + cur[i + 1][j + 1]) % 2);
    }
  }
  return f;
}
int DP(int i, int last) {
  if (i == n) return 0;
  if (dp[i][last] != -1) return dp[i][last];
  int ret = 1000005;
  for (int nxt : v) {
    if (i == 0 or valid(last, nxt))
      ret = min(ret, __builtin_popcount(nxt ^ p[i]) + DP(i + 1, nxt));
  }
  return dp[i][last] = ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  if (n > 3 and m > 3) cout << -1, exit(0);
  for (int i = 0; i < n; i++) cin >> s[i];
  if (m > n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) g[j][i] = s[i][j] - '0';
    swap(n, m);
  } else
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) g[i][j] = s[i][j] - '0';
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) p[i] |= g[i][j] * (1 << j);
  int lim = 1 << m, ans = 1000005, inf = ans;
  for (int i = 0; i < lim; i++) v.push_back(i);
  vector<int> init = {0, 0, 0};
  memset(dp, -1, sizeof dp);
  ans = DP(0, 0);
  if (ans == inf) ans = -1;
  cout << ans << '\n';
  return 0;
}
