#include <bits/stdc++.h>
using namespace std;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) {
  return (b == 0LL ? a : gcd(b, a % b));
}
long long exp(long long a, long long b, long long m) {
  if (b == 0LL) return 1LL;
  if (b == 1LL) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
void print(int i) {
  for (int j = 0; j < 3; j++) {
    cout << !!(i & (1 << j));
  }
  cout << "-";
}
int v[5][5];
int OK[10][10];
int n, m;
bool ok(int a, int b) {
  if (OK[a][b] != -1) return OK[a][b];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 2; j++) {
      int c = (j == 0 ? a : b);
      if ((c & (1 << i))) {
        v[i][j] = 1;
      } else
        v[i][j] = 0;
    }
  }
  for (int i = 0; i < n - 1; i++) {
    int tot = v[i][0] + v[i][1] + v[i + 1][0] + v[i + 1][1];
    if (tot % 2 == 0) return OK[a][b] = 0;
  }
  return OK[a][b] = 1;
}
const int N = 1e6 + 10;
vector<vector<char> > mat;
int C[N][(1 << 3)];
int cost(int id, int msk) {
  if (C[id][msk] != -1) return C[id][msk];
  int c = 0;
  for (int j = 0; j < n; j++) {
    int sla = (msk & (1 << j)) > 0;
    if ((sla) != (mat[j][id] - '0')) c++;
  }
  return C[id][msk] = c;
}
const int inf = 1e8;
int dp[N][(1 << 3)];
int solve(int j, int msk) {
  if (j == m) return 0;
  int& x = dp[j][msk];
  if (x != -1) return x;
  x = inf;
  for (int m = 0; m < (1 << n); m++) {
    if (ok(m, msk)) x = min(x, solve(j + 1, m) + cost(j, m));
  }
  return x;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  memset(C, -1, sizeof(C));
  memset(OK, -1, sizeof(OK));
  memset(dp, -1, sizeof(dp));
  cin >> n >> m;
  mat.resize(n, vector<char>(m));
  for (int i = 0; i < (n); ++i)
    for (int j = 0; j < (m); ++j) cin >> mat[i][j];
  if (n == 1) {
    cout << 0 << "\n";
  } else if (n <= 3) {
    int res = 1e9;
    for (int i = 0; i < (1 << n); i++) {
      res = min(res, solve(1, i) + cost(0, i));
    }
    cout << res << "\n";
  } else {
    cout << -1 << "\n";
  }
}
