#include <bits/stdc++.h>
using namespace std;
bool ok(int a, int b) {
  return (__builtin_popcount(a) + __builtin_popcount(b)) & 1;
}
bool ok(int a, int b, int n) {
  if (n == 3) {
    return ok(a >> 1, b >> 1) & ok(a & 3, b & 3);
  }
  return ok(a, b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(n, vector<int>(m));
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < m; ++j) arr[i][j] = s[j] - '0';
  }
  if (n > 3 && m > 3) {
    cout << -1 << endl;
    return 0;
  }
  if (n > m) {
    vector<vector<int>> tmp(m, vector<int>(n));
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) tmp[j][i] = arr[i][j];
    arr = std::move(tmp);
    swap(n, m);
  }
  if (n == 1 || m == 1) {
    cout << 0 << endl;
    return 0;
  }
  const int inf = 1000000000;
  int k = (1 << n);
  vector<vector<int>> dp(m + 1, vector<int>(k, inf));
  for (int j = 0; j < k; ++j) dp[0][j] = 0;
  vector<vector<char>> good(k, vector<char>(k));
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < k; ++j) good[i][j] = ok(i, j, n);
  for (int i = 1; i <= m; ++i) {
    for (int cur = 0; cur < k; ++cur)
      for (int prev = 0; prev < k; ++prev) {
        if (!good[cur][prev]) continue;
        int diff = 0;
        for (int j = 0; j < n; ++j) diff += ((cur >> j) & 1) != arr[j][i - 1];
        dp[i][cur] = min(dp[i][cur], dp[i - 1][prev] + diff);
      }
  }
  int ans = inf;
  for (int j = 0; j < k; ++j) ans = min(ans, dp[m][j]);
  if (ans == inf) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans << endl;
  return 0;
}
