#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long __64inf = 0x3f3f3f3f3f3f3f3f;
const int MAX = 2e6 + 50;
const int mod = 1e9 + 7;
void file_read() {}
char s[10][MAX];
int dp[MAX][10];
int n, m;
int cost(int col, int mask) {
  int res = 0;
  for (int i = 1; i <= n; i++) {
    int x = s[i][col] - '0';
    int y = mask & 1;
    res += x != y;
    mask >>= 1;
  }
  return res;
}
bool check(int x, int y) {
  int A[2][10];
  for (int i = 1; i <= n; i++) {
    int a = x & 1;
    int b = y & 1;
    A[0][i] = a;
    A[1][i] = b;
    x >>= 1;
    y >>= 1;
  }
  int res = A[0][1] ^ A[0][2] ^ A[1][1] ^ A[1][2];
  if (!res) return false;
  if (n == 2) return true;
  res = A[0][2] ^ A[0][3] ^ A[1][2] ^ A[1][3];
  return res;
}
int main() {
  file_read();
  scanf("%d%d", &n, &m);
  if (n == 1 || m == 1) {
    puts("0");
    return 0;
  }
  if (n >= 4 and m >= 4) {
    puts("-1");
    return 0;
  }
  if (n <= m) {
    for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1);
  } else {
    for (int i = 1; i <= n; i++) {
      char t[10];
      scanf("%s", t + 1);
      for (int j = 1; j <= m; j++) {
        s[j][i] = t[j];
      }
    }
    swap(n, m);
  }
  memset(dp, 0x3f, sizeof(dp));
  int up = n == 2 ? 4 : 8;
  for (int i = 0; i < up; i++) {
    for (int j = 0; j < up; j++) {
      if (check(i, j)) dp[2][j] = min(dp[2][j], cost(1, i) + cost(2, j));
    }
  }
  for (int i = 3; i <= m; i++) {
    for (int x = 0; x < up; x++) {
      for (int y = 0; y < up; y++) {
        if (check(x, y)) dp[i][y] = min(dp[i][y], dp[i - 1][x] + cost(i, y));
      }
    }
  }
  int res = inf;
  for (int i = 0; i < up; i++) {
    res = min(res, dp[m][i]);
  }
  printf("%d\n", res);
}
