#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
const int M = 2520100;
const int mod = 1e9 + 7;
const long long MOD = 1e9 + 6;
const int inf = 1e9 + 7;
mt19937 gen(time(0));
vector<vector<int> > a, b;
int dp[N][10], kol[N][10];
vector<int> g[10];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  int n, m;
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    a[i].resize(m);
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      a[i][j] = int32_t(c - '0');
    }
  }
  if (n >= 4 && m >= 4) {
    cout << -1;
    return 0;
  }
  if (n > m) {
    int str = 0;
    b.resize(m);
    for (int i = 0; i < m; i++) b[i].resize(n);
    for (int j = m - 1; j >= 0; j--) {
      for (int i = 0; i < n; i++) {
        b[str][i] = a[i][j];
      }
      str++;
    }
    swap(n, m);
    a = b;
  }
  for (int mask = 0; mask < (1 << n); mask++) {
    for (int ms = 0; ms < (1 << n); ms++) {
      bool fl = 0;
      for (int i = 0; i + 1 < n; i++) {
        int k = 0;
        if (mask & (1 << i)) k++;
        if (mask & (1 << (i + 1))) k++;
        if (ms & (1 << i)) k++;
        if (ms & (1 << (i + 1))) k++;
        if (k % 2 == 0) {
          fl = 1;
          break;
        }
      }
      if (fl) continue;
      g[mask].push_back(ms);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      dp[i][mask] = inf;
      for (int j = 0; j < n; j++) {
        int c = 0;
        if (mask & (1 << j)) c = 1;
        if (c != a[j][i]) kol[i][mask]++;
      }
      if (!i) dp[i][mask] = kol[i][mask];
    }
  }
  for (int i = 1; i < m; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (dp[i - 1][mask] == inf) continue;
      for (auto to : g[mask]) {
        dp[i][to] = min(dp[i][to], dp[i - 1][mask] + kol[i][to]);
      }
    }
  }
  int ans = inf;
  for (int mask = 0; mask < (1 << n); mask++) {
    ans = min(ans, dp[m - 1][mask]);
  }
  if (ans == inf)
    cout << -1;
  else
    cout << ans;
}
