#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e18;
const int N = 1e6;
long long cntBIT(long long x) { return __builtin_popcountll(x); }
long long a[N + 13];
long long dp[N + 13][(1 << (3)) + 13];
int n, m;
bool check(long long mask) {
  for (int i = 1; i < m; ++i)
    if (((mask >> i) & 1) == ((mask >> i - 1) & 1)) return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  if (min(n, m) > 3) return cout << -1, 0;
  if (min(n, m) == 1) return cout << 0, 0;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      char x;
      cin >> x;
      long long k = x - '0';
      if (k) {
        if (n > m)
          a[i] |= (1 << (j - 1));
        else
          a[j] |= (1 << (i - 1));
      }
    }
  if (m > n) swap(n, m);
  for (int mask = 0; mask < (1 << (m)); ++mask)
    for (int i = 0; i <= n; ++i) dp[i][mask] = oo;
  for (int mask = 0; mask < (1 << (m)); ++mask)
    dp[1][mask] = cntBIT(mask ^ a[1]);
  for (int i = 2; i <= n; ++i)
    for (int mask = 0; mask < (1 << (m)); ++mask)
      for (int state = 0; state < (1 << (m)); ++state)
        if (check(mask ^ state))
          dp[i][mask] =
              min(dp[i][mask], cntBIT(mask ^ a[i]) + dp[i - 1][state]);
  long long res = oo;
  for (int mask = 0; mask < (1 << (m)); ++mask) res = min(res, dp[n][mask]);
  cout << res << "\n";
}
