#include <bits/stdc++.h>
using namespace std;
const int mx = 1e6 + 1;
int n, m;
string s[3];
int dp[mx][8];
bool isok(int i, int j) {
  int cnt = 0;
  for (int k = 0; k < 2; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1);
  if (cnt % 2 == 0) return false;
  if (n == 2) return true;
  cnt = 0;
  for (int k = 1; k < 3; k++) cnt += ((i >> k) & 1) + ((j >> k) & 1);
  return cnt & 1;
}
int solve(int idx, int msk) {
  if (idx == m) return 0;
  int &ans = dp[idx][msk];
  if (~ans) return ans;
  ans = 1e9;
  for (int i = 0; i < (1 << n); i++)
    if (idx == 0 || isok(msk, i)) {
      int cur = 0;
      for (int j = 0; j < n; j++) cur += ((i >> j) & 1) != (s[j][idx] - '0');
      ans = min(ans, cur + solve(idx + 1, i));
    }
  return ans;
}
int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  if (n == 1) return cout << "0", 0;
  if (n >= 4) return cout << "-1", 0;
  for (int i = 0; i < n; i++) cin >> s[i];
  int ans = 1e9;
  memset(dp, -1, sizeof dp);
  for (int i = 0; i < (1 << n); i++) ans = min(ans, solve(0, i));
  cout << ans;
  return 0;
}
