#include <bits/stdc++.h>
using namespace std;
int read() {
  int ans = 0, f = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + c - '0';
    c = getchar();
  }
  return ans * f;
}
const int N = 1e6 + 5;
int n, m, mmap1[4][N], mmap2[N][4], dp[N][2][2][2], ans;
char s[N];
int main() {
  n = read();
  m = read();
  if (n >= 4 && m >= 4)
    printf("-1\n");
  else if (n == 1 || m == 1)
    printf("0\n");
  else if (n == 2) {
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) mmap1[i][j] = s[j] - '0';
    }
    for (int i = 1; i <= m; ++i) {
      dp[i][0][0][0] = min(dp[i - 1][1][0][0], dp[i - 1][0][1][0]);
      dp[i][0][1][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][0]);
      dp[i][1][0][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][0]);
      dp[i][1][1][0] = min(dp[i - 1][1][0][0], dp[i - 1][0][1][0]);
      if (mmap1[1][i] == 1)
        dp[i][0][0][0]++, dp[i][0][1][0]++;
      else
        dp[i][1][0][0]++, dp[i][1][1][0]++;
      if (mmap1[2][i] == 1)
        dp[i][0][0][0]++, dp[i][1][0][0]++;
      else
        dp[i][0][1][0]++, dp[i][1][1][0]++;
    }
    ans = 0x7fffffff;
    ans = min(ans, dp[m][0][0][0]);
    ans = min(ans, dp[m][0][1][0]);
    ans = min(ans, dp[m][1][0][0]);
    ans = min(ans, dp[m][1][1][0]);
    printf("%d\n", ans);
  } else if (m == 2) {
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) mmap2[i][j] = s[j] - '0';
    }
    for (int i = 1; i <= n; ++i) {
      dp[i][0][0][0] = min(dp[i - 1][1][0][0], dp[i - 1][0][1][0]);
      dp[i][0][1][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][0]);
      dp[i][1][0][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][0]);
      dp[i][1][1][0] = min(dp[i - 1][1][0][0], dp[i - 1][0][1][0]);
      if (mmap2[i][1] == 1)
        dp[i][0][0][0]++, dp[i][0][1][0]++;
      else
        dp[i][1][0][0]++, dp[i][1][1][0]++;
      if (mmap2[i][2] == 1)
        dp[i][0][0][0]++, dp[i][1][0][0]++;
      else
        dp[i][0][1][0]++, dp[i][1][1][0]++;
    }
    ans = 0x7fffffff;
    ans = min(ans, dp[n][0][0][0]);
    ans = min(ans, dp[n][0][1][0]);
    ans = min(ans, dp[n][1][0][0]);
    ans = min(ans, dp[n][1][1][0]);
    printf("%d\n", ans);
  } else if (n == 3) {
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) mmap1[i][j] = s[j] - '0';
    }
    for (int i = 1; i <= m; ++i) {
      dp[i][0][0][0] = min(dp[i - 1][1][0][1], dp[i - 1][0][1][0]);
      dp[i][0][0][1] = min(dp[i - 1][0][1][1], dp[i - 1][1][0][0]);
      dp[i][0][1][0] = min(dp[i - 1][1][1][1], dp[i - 1][0][0][0]);
      dp[i][1][0][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][1]);
      dp[i][0][1][1] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][1]);
      dp[i][1][0][1] = min(dp[i - 1][1][1][1], dp[i - 1][0][0][0]);
      dp[i][1][1][0] = min(dp[i - 1][0][1][1], dp[i - 1][1][0][0]);
      dp[i][1][1][1] = min(dp[i - 1][0][1][0], dp[i - 1][1][0][1]);
      if (mmap1[1][i] == 1)
        dp[i][0][0][0]++, dp[i][0][0][1]++, dp[i][0][1][0]++, dp[i][0][1][1]++;
      else
        dp[i][1][0][0]++, dp[i][1][0][1]++, dp[i][1][1][0]++, dp[i][1][1][1]++;
      if (mmap1[2][i] == 1)
        dp[i][0][0][0]++, dp[i][0][0][1]++, dp[i][1][0][0]++, dp[i][1][0][1]++;
      else
        dp[i][0][1][0]++, dp[i][0][1][1]++, dp[i][1][1][0]++, dp[i][1][1][1]++;
      if (mmap1[3][i] == 1)
        dp[i][0][0][0]++, dp[i][0][1][0]++, dp[i][1][0][0]++, dp[i][1][1][0]++;
      else
        dp[i][0][0][1]++, dp[i][0][1][1]++, dp[i][1][0][1]++, dp[i][1][1][1]++;
    }
    ans = 0x7fffffff;
    ans = min(ans, dp[m][0][0][0]);
    ans = min(ans, dp[m][0][0][1]);
    ans = min(ans, dp[m][0][1][0]);
    ans = min(ans, dp[m][1][0][0]);
    ans = min(ans, dp[m][0][1][1]);
    ans = min(ans, dp[m][1][0][1]);
    ans = min(ans, dp[m][1][1][0]);
    ans = min(ans, dp[m][1][1][1]);
    printf("%d\n", ans);
  } else {
    for (int i = 1; i <= n; ++i) {
      scanf("%s", s + 1);
      for (int j = 1; j <= m; ++j) mmap2[i][j] = s[j] - '0';
    }
    for (int i = 1; i <= n; ++i) {
      dp[i][0][0][0] = min(dp[i - 1][1][0][1], dp[i - 1][0][1][0]);
      dp[i][0][0][1] = min(dp[i - 1][0][1][1], dp[i - 1][1][0][0]);
      dp[i][0][1][0] = min(dp[i - 1][1][1][1], dp[i - 1][0][0][0]);
      dp[i][1][0][0] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][1]);
      dp[i][0][1][1] = min(dp[i - 1][1][1][0], dp[i - 1][0][0][1]);
      dp[i][1][0][1] = min(dp[i - 1][1][1][1], dp[i - 1][0][0][0]);
      dp[i][1][1][0] = min(dp[i - 1][0][1][1], dp[i - 1][1][0][0]);
      dp[i][1][1][1] = min(dp[i - 1][0][1][0], dp[i - 1][1][0][1]);
      if (mmap2[i][1] == 1)
        dp[i][0][0][0]++, dp[i][0][0][1]++, dp[i][0][1][0]++, dp[i][0][1][1]++;
      else
        dp[i][1][0][0]++, dp[i][1][0][1]++, dp[i][1][1][0]++, dp[i][1][1][1]++;
      if (mmap2[i][2] == 1)
        dp[i][0][0][0]++, dp[i][0][0][1]++, dp[i][1][0][0]++, dp[i][1][0][1]++;
      else
        dp[i][0][1][0]++, dp[i][0][1][1]++, dp[i][1][1][0]++, dp[i][1][1][1]++;
      if (mmap2[i][3] == 1)
        dp[i][0][0][0]++, dp[i][0][1][0]++, dp[i][1][0][0]++, dp[i][1][1][0]++;
      else
        dp[i][0][0][1]++, dp[i][0][1][1]++, dp[i][1][0][1]++, dp[i][1][1][1]++;
    }
    ans = 0x7fffffff;
    ans = min(ans, dp[n][0][0][0]);
    ans = min(ans, dp[n][0][0][1]);
    ans = min(ans, dp[n][0][1][0]);
    ans = min(ans, dp[n][1][0][0]);
    ans = min(ans, dp[n][0][1][1]);
    ans = min(ans, dp[n][1][0][1]);
    ans = min(ans, dp[n][1][1][0]);
    ans = min(ans, dp[n][1][1][1]);
    printf("%d\n", ans);
  }
  return 0;
}
