#include <bits/stdc++.h>
long long mod = 1000000007;
long long mod2 = 998244353;
long long OO = 1e18;
long long david_villa_pow(long long x, long long n) {
  if (n == 0) return 1;
  long long u = david_villa_pow(x, n / 2);
  u = ((u % mod) * (u % mod)) % mod;
  if (n % 2 == 1) u = ((u % mod) * (x % mod)) % mod;
  return u % mod;
}
long long up(long long x, long long y) {
  if (x % y == 0) return x / y;
  return x / y + 1;
}
using namespace std;
char c[1000100][5];
char c1[5][1000100];
int n, m;
int dp[1000010][8];
int no(char x) {
  if (x == '1') return 0;
  return 1;
}
int corr(char x) {
  if (x == '1') return 1;
  return 0;
}
bool chk(int i, int r, int indx) {
  if (m == 1) return 1;
  if (m == 2) {
    int cur = r;
    int ones = 0;
    if (cur >= 2) {
      if (no(c[i - 1][1]) == 1) ones++;
      cur -= 2;
    } else {
      if (corr(c[i - 1][1]) == 1) ones++;
    }
    if (cur >= 1) {
      if (no(c[i - 1][2]) == 1) ones++;
      cur -= 1;
    } else {
      if (corr(c[i - 1][2]) == 1) ones++;
    }
    cur = indx;
    if (cur >= 2) {
      if (no(c[i][1]) == 1) ones++;
      cur -= 2;
    } else {
      if (corr(c[i][1]) == 1) ones++;
    }
    if (cur >= 1) {
      if (no(c[i][2]) == 1) ones++;
      cur -= 1;
    } else {
      if (corr(c[i][2]) == 1) ones++;
    }
    if (ones % 2 == 0) return 0;
    return 1;
  }
  int ones = 0;
  int cur = r;
  if (cur >= 4) {
    if (no(c[i - 1][1]) == 1) ones++;
    cur -= 4;
  } else {
    if (corr(c[i - 1][1]) == 1) ones++;
  }
  if (cur >= 2) {
    if (no(c[i - 1][2]) == 1) ones++;
    cur -= 2;
  } else {
    if (corr(c[i - 1][2]) == 1) ones++;
  }
  cur = indx;
  if (cur >= 4) {
    if (no(c[i][1]) == 1) ones++;
    cur -= 4;
  } else {
    if (corr(c[i][1]) == 1) ones++;
  }
  if (cur >= 2) {
    if (no(c[i][2]) == 1) ones++;
    cur -= 2;
  } else {
    if (corr(c[i][2]) == 1) ones++;
  }
  if (ones % 2 == 0) return 0;
  cur = r;
  ones = 0;
  if (cur >= 4) cur -= 4;
  if (cur >= 2) {
    if (no(c[i - 1][2]) == 1) ones++;
    cur -= 2;
  } else {
    if (corr(c[i - 1][2]) == 1) ones++;
  }
  if (cur >= 1) {
    if (no(c[i - 1][3]) == 1) ones++;
    cur -= 1;
  } else {
    if (corr(c[i - 1][3]) == 1) ones++;
  }
  cur = indx;
  if (cur >= 4) cur -= 4;
  if (cur >= 2) {
    if (no(c[i][2]) == 1) ones++;
    cur -= 2;
  } else {
    if (corr(c[i][2]) == 1) ones++;
  }
  if (cur >= 1) {
    if (no(c[i][3]) == 1) ones++;
    cur -= 1;
  } else {
    if (corr(c[i][3]) == 1) ones++;
  }
  if (ones % 2 == 0) return 0;
  return 1;
}
int getChange(int indx) {
  int res = 0;
  if (indx >= 4) {
    res++;
    indx -= 4;
  }
  if (indx >= 2) {
    res++;
    indx -= 2;
  }
  if (indx >= 1) res++;
  return res;
}
int Die(int i, int r, int val) {
  if (i == n + 1) return 0;
  if (dp[i][r] + 1) return dp[i][r];
  int res = 1e9;
  for (int indx = 0; indx <= val; indx++) {
    if (i != 1) {
      if (!chk(i, r, indx)) continue;
    }
    int change = getChange(indx);
    res = min(res, Die(i + 1, indx, val) + change);
  }
  return dp[i][r] = res;
}
void El_MaraVilla() {
  long long i, j;
  ;
  scanf("%d%d", &n, &m);
  if (n >= 4 && m >= 4) {
    char x;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= m; j++) scanf(" %c", &x);
    printf("-1");
  } else {
    if (n > m) {
      for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++) scanf(" %c", &c[i][j]);
    } else {
      for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++) scanf(" %c", &c1[i][j]);
      for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++) c[j][i] = c1[i][j];
      swap(n, m);
    }
    int mx = (1 << m) - 1;
    memset(dp, -1, sizeof dp);
    printf("%d", Die(1, 0, mx));
  }
}
int main() {
  long long i, j;
  ;
  long long T = 1;
  while (T--) {
    El_MaraVilla();
  }
  return 0;
}
