#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool f = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = 1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return f ? -x : x;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
void writeln(long long x) {
  write(x);
  putchar('\n');
}
void writep(long long x) {
  write(x);
  putchar(' ');
}
long long const N = 1e6 + 3;
long long n, m, a[4][N], f[N][2][2][2];
char s[N];
signed main() {
  n = read();
  m = read();
  if (n >= 4 && m >= 4) return puts("-1"), 0;
  if (n > m) {
    for (long long i = 1; i <= n; i++) {
      scanf("%s", s + 1);
      for (long long j = 1; j <= m; j++) a[j][i] = s[j] ^ 48;
    }
    swap(n, m);
  } else {
    for (long long i = 1; i <= n; i++) {
      scanf("%s", s + 1);
      for (long long j = 1; j <= m; j++) a[i][j] = s[j] ^ 48;
    }
  }
  if (n == 1) return puts("0"), 0;
  if (n == 2) {
    memset(f, 0x3f, sizeof(f));
    for (long long i = 0; i <= 1; i++)
      for (long long j = 0; j <= 1; j++)
        f[1][i][j][0] = (a[1][1] != i) + (a[2][1] != j);
    for (long long i = 2; i <= m; i++)
      for (long long j = 0; j <= 1; j++)
        for (long long k = 0; k <= 1; k++)
          for (long long p = 0; p <= 1; p++)
            for (long long l = 0; l <= 1; l++)
              if ((j + k + p + l) & 1)
                f[i][j][k][0] =
                    min(f[i][j][k][0],
                        f[i - 1][p][l][0] + (j != a[1][i]) + (k != a[2][i]));
    long long ans = 1e9;
    for (long long i = 0; i <= 1; i++)
      for (long long j = 0; j <= 1; j++) ans = min(ans, f[m][i][j][0]);
    writeln(ans);
    return 0;
  }
  if (n == 3) {
    memset(f, 0x3f, sizeof(f));
    for (long long i = 0; i <= 1; i++)
      for (long long j = 0; j <= 1; j++)
        for (long long k = 0; k <= 1; k++)
          f[1][i][j][k] = (a[1][1] != i) + (a[2][1] != j) + (a[3][1] != k);
    for (long long i = 2; i <= m; i++)
      for (long long j = 0; j <= 1; j++)
        for (long long k = 0; k <= 1; k++)
          for (long long p = 0; p <= 1; p++)
            for (long long l = 0; l <= 1; l++)
              for (long long h = 0; h <= 1; h++)
                for (long long g = 0; g <= 1; g++)
                  if (((j + k + l + h) & 1) && ((k + p + h + g) & 1))
                    f[i][j][k][p] =
                        min(f[i][j][k][p], f[i - 1][l][h][g] + (a[1][i] != j) +
                                               (a[2][i] != k) + (a[3][i] != p));
    long long ans = 1e9;
    for (long long i = 0; i <= 1; i++)
      for (long long j = 0; j <= 1; j++)
        for (long long k = 0; k <= 1; k++) ans = min(ans, f[m][i][j][k]);
    writeln(ans);
  }
  return 0;
}
