#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const int maxm = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const long long LINF = 3e17 + 1;
const long long mod = 1e9 + 7;
int n, r, m, h;
inline int read() {
  register int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return (f == 1) ? x : -x;
}
int f[maxn][8], c[8][8];
char s[4][maxn];
int main() {
  n = read(), m = read();
  if (n == 1 || m == 1) {
    printf("0\n");
    return 0;
  }
  if (n >= 4) {
    printf("-1\n");
    return 0;
  }
  for (int i = 0; i < n; i++) scanf("%s", s[i] + 1);
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j < (1 << n); j++) {
      int a1 = i & 1, a2 = (bool)(i & 2), a3 = (bool)(i & 4);
      int b1 = j & 1, b2 = (bool)(j & 2), b3 = (bool)(j & 4);
      if ((a1 + a2 + b1 + b2) % 2 && ((n == 2) || (a2 + a3 + b2 + b3) % 2))
        c[i][j] = 1;
    }
    for (int j = 0; j < n; j++) {
      if (s[j][1] != '0' + ((i >> (n - j - 1)) & 1)) f[1][i]++;
    }
  }
  for (int i = 2; i <= m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      f[i][j] = n * m + 1;
      for (int k = 0; k < (1 << n); k++) {
        if (c[k][j]) f[i][j] = min(f[i][j], f[i - 1][k]);
      }
      for (int k = 0; k < n; k++) {
        if (s[k][i] != '0' + ((j >> (n - k - 1)) & 1)) f[i][j]++;
      }
    }
  }
  int ans = n * m + 1;
  for (int i = 0; i < (1 << n); i++) ans = min(ans, f[m][i]);
  if (ans == m * n + 1)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
