#include <bits/stdc++.h>
using namespace std;
int n, m, mp[5][1000050];
int sta[20], cnt[20], tot;
char s[1000050];
int f[1000050][20], ans;
void init() {
  for (int i = 0; i < (1 << n); i++) {
    sta[++tot] = i;
    int u = i;
    while (u) {
      cnt[tot] += ((u % 2) ^ 0);
      u >>= 1;
    }
  }
}
int change(int id, int y) {
  int ans = 0, status = sta[id];
  for (int i = n; i >= 1; i--, status >>= 1) ans += ((status % 2) ^ mp[i][y]);
  return ans;
}
void dp() {
  init();
  memset(f, 0x3f, sizeof f);
  for (int i = 1; i <= tot; i++) f[1][i] = change(i, 1);
  for (int i = 2; i <= m; i++)
    for (int j = 1; j <= tot; j++)
      for (int k = 1; k <= tot; k++) {
        int a = sta[j], b = sta[k];
        int last = a % 2 + b % 2, now, flag = 1;
        a >>= 1;
        b >>= 1;
        for (int l = n - 1; l >= 1; l--) {
          now = a % 2 + b % 2;
          a >>= 1;
          b >>= 1;
          if ((last + now) % 2 == 0) flag = 0;
          last = now;
        }
        if (flag) f[i][j] = min(f[i][j], f[i - 1][k] + change(j, i));
      }
}
int main() {
  scanf("%d%d", &n, &m);
  if (min(n, m) >= 4) {
    printf("-1");
    return 0;
  }
  if (n == 1) {
    printf("0");
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    for (int j = 1; j <= m; j++) mp[i][j] = s[j] - '0';
  }
  dp();
  ans = n * m;
  for (int i = 1; i <= tot; i++) ans = min(ans, f[m][i]);
  printf("%d", ans);
  return 0;
}
