#include <bits/stdc++.h>
using namespace std;
const int N = 1000000 + 10;
const int Mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
int v[N], f[N][10];
char s[N];
bool check(int a, int b) {
  if (n == 2) return (__builtin_popcount(a) + __builtin_popcount(b)) % 2;
  return (__builtin_popcount(a >> 1) + __builtin_popcount(b >> 1)) % 2 &&
         (__builtin_popcount(a & 3) + __builtin_popcount(b & 3)) % 2;
  ;
}
void gan() {
  scanf("%d %d", &n, &m);
  if (n >= 4) {
    puts("-1");
    return;
  }
  if (n == 1 || m == 1) {
    puts("0");
    return;
  }
  for (int i = 0; i < n; i++) {
    scanf("%s", s);
    for (int j = 0; j < m; j++)
      if (s[j] == '1') v[j] |= (1 << i);
  }
  memset(f, inf, sizeof f);
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (i == 0)
        f[i][j] = 0;
      else {
        for (int k = 0; k < (1 << n); k++) {
          if (check(j, k)) f[i][j] = min(f[i][j], f[i - 1][k]);
        }
      }
      f[i][j] += __builtin_popcount(v[i] ^ j);
    }
  }
  int res = inf;
  for (int i = 0; i < (1 << n); i++) res = min(res, f[m - 1][i]);
  printf("%d\n", res);
}
int main() {
  int T = 1;
  while (T--) {
    gan();
  }
}
