#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6 + 20;
long long inf = (long long)1e9;
long long mod = (long long)1e9 + 7;
long long n, m, a[3][MAX], dp[MAX][8];
long long c(long long num) {
  long long ret = 0;
  for (int i = 0; i < 5; i++) {
    if ((1 << i) & num) ret++;
  }
  return ret;
}
long long pos(long long num_1, long long num_2) {
  long long ret = 1;
  for (int i = 0; i < n - 1; i++) {
    long long cnt = (((1 << i) & num_1) != 0) + (((1 << (i + 1)) & num_1) != 0);
    cnt += (((1 << i) & num_2) != 0) + (((1 << (i + 1)) & num_2) != 0);
    ret = min(ret, cnt % 2);
  }
  return ret;
}
void solve() {
  cin >> n >> m;
  if (0) {
    cout << -1;
    return;
  } else {
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
      for (int j = 0; j < m; j++) {
        long long val = s[j] - 48;
        a[min(i, 2)][j] = val;
      }
    }
    if (n > 3) {
      cout << -1 << "\n";
      return;
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < (1 << n); j++) dp[i][j] = inf;
    }
    for (int i = 0; i < m; i++) {
      long long val = 0;
      for (int j = 0; j < n; j++) {
        if (a[j][i]) val += (1 << j);
      }
      if (i == 0) {
        for (int j = 0; j < (1 << n); j++) {
          dp[i][j] = c(j ^ val);
        }
        continue;
      }
      for (int j = 0; j < (1 << n); j++) {
        for (int z = 0; z < (1 << n); z++) {
          if (pos(z, j)) {
            dp[i][j] = min(dp[i][j], dp[i - 1][z] + c(j ^ val));
          }
        }
      }
    }
    long long ans = inf;
    for (int i = 0; i < (1 << n); i++) ans = min(ans, dp[m - 1][i]);
    if (ans == inf)
      cout << -1;
    else
      cout << ans;
  }
}
int main() {
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  ios::sync_with_stdio(0);
  long long q;
  q = 1;
  while (q--) solve();
  return 0;
}
