#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[4][1000005];
int f[8][2];
int num[8];
int c[4][4];
int main() {
  scanf("%d%d", &n, &m);
  if (n >= 4 && m >= 4) {
    printf("-1\n");
    return 0;
  }
  if (n == 1 || m == 1) {
    printf("0\n");
    return 0;
  }
  if (n < 4) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        scanf("%1d", &a[i][j]);
      }
    }
  } else {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= m; ++j) {
        scanf("%1d", &a[j][i]);
      }
    }
    swap(n, m);
  }
  int cur = 1;
  num[1] = num[2] = num[4] = 1, num[3] = num[5] = num[6] = 2, num[7] = 3;
  for (int i = 0; i < (1 << n); ++i) f[i][cur] = num[i];
  for (int i = 2; i <= m; ++i) {
    cur ^= 1;
    for (int j = 0; j < (1 << n); ++j) {
      f[j][cur] = 0x3f3f3f3f;
      for (int k = 0; k < (1 << n); ++k) {
        bool mark = false;
        for (int l = 1; l <= n; ++l) {
          if (j & (1 << (l - 1)))
            c[l][2] = a[l][i] ^ 1;
          else
            c[l][2] = a[l][i];
          if (k & (1 << (l - 1)))
            c[l][1] = a[l][i - 1] ^ 1;
          else
            c[l][1] = a[l][i - 1];
          if (l >= 2) {
            if ((c[l][2] + c[l][1] + c[l - 1][2] + c[l - 1][1]) % 2 == 0)
              mark = true;
          }
        }
        if (!mark) {
          f[j][cur] = min(f[j][cur], f[k][cur ^ 1] + num[j]);
        }
      }
    }
  }
  int ans = 0x3f3f3f3f;
  for (int i = 0; i < (1 << n); ++i) ans = min(ans, f[i][cur]);
  if (ans == 0x3f3f3f3f)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
