#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
inline bool compaitable(int a, int b, int n);
int main() {
  int n, m;
  cin >> n >> m;
  vector<string> v(n);
  for (string &i : v) cin >> i;
  if ((n >= 4 && m >= 4)) {
    cout << -1 << endl;
    exit(0);
  } else if (n == 1) {
    cout << 0 << endl;
    exit(0);
  }
  vector<vector<int>> dp(m, vector<int>(1 << n, inf));
  for (int j = 0; j < 1 << n; j++) {
    int to_change = 0;
    for (int bit = 0; bit < n; bit++) {
      if (j & (1 << bit))
        to_change += (v[bit][0] == '0');
      else
        to_change += (v[bit][0] == '1');
    }
    dp[0][j] = to_change;
  }
  for (int i = 1; i < m; i++) {
    for (int j = 0; j < (1 << n); j++) {
      int to_change = 0;
      for (int bit = 0; bit < n; bit++) {
        if (j & (1 << bit))
          to_change += (v[bit][i] == '0');
        else
          to_change += (v[bit][i] == '1');
      }
      for (int k = 0; k < 1 << n; k++) {
        if (compaitable(k, j, n)) {
          dp[i][j] = min(dp[i][j], dp[i - 1][k] + to_change);
        }
      }
    }
  }
  cout << *min_element(dp[m - 1].begin(), dp[m - 1].end()) << endl;
  return 0;
}
inline bool compaitable(int a, int b, int n) {
  if (n == 2) {
    return (__builtin_popcount(a) + __builtin_popcount(b)) % 2 == 1;
  }
  for (int j = 1; j < 3; j++) {
    int ones = 0;
    ones += ((a & (1 << j)) > 0);
    ones += ((b & (1 << (j))) > 0);
    ones += ((a & (1 << (j - 1))) > 0);
    ones += ((b & (1 << (j - 1))) > 0);
    if (ones % 2 == 0) {
      return false;
    }
  }
  return true;
}
