#include <bits/stdc++.h>
const int maxn = 1e6 + 10;
const int mod = 998244353;
using namespace std;
int n, m;
vector<int> h[maxn];
int a[4][maxn], dp[maxn][10];
void solve() {
  scanf("%d%d", &n, &m);
  if (n >= 4 && m >= 4) {
    printf("-1");
    return;
  }
  if (n == 1 || m == 1) {
    printf("0");
    return;
  }
  for (int i = 1, x; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (n > m)
        scanf("%1d", &a[j][i]);
      else
        scanf("%1d", &a[i][j]);
  if (n > m) swap(n, m);
  for (int i = 0; i < 1 << n; i++) {
    int cnt = 0;
    for (int j = 1; j <= n; j++)
      if (a[j][1] ^ ((i >> j - 1) & 1)) cnt++;
    dp[1][i] = cnt;
  }
  for (int i = 2; i <= m; i++) {
    for (int j = 0; j < 1 << n; j++) dp[i][j] = 0x3f3f3f3f;
    for (int j = 0; j < 1 << n; j++) {
      int cnt = 0, A = 0, B = 0, C = 0, D = 0;
      for (int k = 0; k < 2; k++) A += j >> k & 1;
      for (int k = 1; k < 3; k++) B += j >> k & 1;
      for (int k = 0; k < 1 << n; k++) {
        C = 0, D = 0, cnt = 0;
        for (int l = 1; l <= n; l++)
          if (a[l][i] ^ ((k >> l - 1) & 1)) cnt++;
        for (int l = 0; l < 2; l++) C += k >> l & 1;
        for (int l = 1; l < 3; l++) D += k >> l & 1;
        if (n == 2 && (A + C) % 2 == 1 ||
            n == 3 && (A + C) % 2 == 1 && (B + D) % 2 == 1)
          dp[i][k] = min(dp[i][k], dp[i - 1][j] + cnt);
      }
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < 1 << n; i++) ans = min(ans, dp[m][i]);
  printf("%d\n", ans);
}
int main() {
  solve();
  return 0;
}
