#include <bits/stdc++.h>
using namespace std;
template <class T>
void chkmax(T& x, T y) {
  x = x > y ? x : y;
}
template <class T>
void chkmin(T& x, T y) {
  x = x < y ? x : y;
}
int n, m;
vector<string> s;
int calc(int mask, int j) {
  int res = 0;
  for (int i = 0; i < n; ++i)
    if ((mask >> i & 1) != (s[i][j] - '0')) ++res;
  return res;
}
bool check(int a, int b) {
  for (int i = 0; i < n - 1; ++i)
    if (((a >> i & 1) + (a >> i + 1 & 1) + (b >> i & 1) + (b >> i + 1 & 1)) %
            2 ==
        0)
      return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cin >> n >> m;
  if (n >= 4) {
    cout << -1 << '\n';
    return 0;
  }
  s.resize(n);
  for (int i = 0; i < n; ++i) cin >> s[i];
  vector<vector<int>> dp(m, vector<int>(1 << n, 1e9));
  for (int mask = 0; mask < 1 << n; ++mask) dp[0][mask] = calc(mask, 0);
  for (int i = 0; i < m - 1; ++i)
    for (int c_mask = 0; c_mask < 1 << n; ++c_mask)
      for (int n_mask = 0; n_mask < 1 << n; ++n_mask)
        if (check(c_mask, n_mask))
          chkmin(dp[i + 1][n_mask], dp[i][c_mask] + calc(n_mask, i + 1));
  int ans = 1e9;
  for (int mask = 0; mask < 1 << n; ++mask) chkmin(ans, dp[m - 1][mask]);
  cout << ans << '\n';
  return 0;
}
