#include <bits/stdc++.h>
#pragma GCC optimize("O2,unroll-loops")
using namespace std;
const int inf = 1000000010;
const long long INF = 10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 1000010, LOG = 20;
int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN];
int dp[MAXN][8];
bool ok[8][8];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  if (n == 1) return cout << 0 << '\n', 0;
  if (n >= 4) return cout << -1 << '\n', 0;
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= m; j++) {
      char ch;
      cin >> ch;
      A[j] |= ((ch - '0') << i);
    }
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < (1 << n); j++) {
      ok[i][j] = 1;
      for (int k = 1; k < n; k++) {
        int sum =
            (i >> k & 1) + (i << 1 >> k & 1) + (j >> k & 1) + (j << 1 >> k & 1);
        if (sum % 2 == 0) ok[i][j] = 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      dp[i][j] = inf;
      int cost = __builtin_popcount(A[i] ^ j);
      for (int jj = 0; jj < (1 << n); jj++)
        if (ok[j][jj]) dp[i][j] = min(dp[i][j], dp[i - 1][jj] + cost);
    }
  }
  ans = inf;
  for (int j = 0; j < (1 << n); j++) ans = min(ans, dp[m][j]);
  cout << ans << "\n";
  return 0;
}
