#include <bits/stdc++.h>
using namespace std;
bool compatible(int a, int b, int sz) {
  int cnt1 = 0;
  cnt1 += (a >> 0 & 1);
  cnt1 += (a >> 1 & 1);
  cnt1 += (b >> 0 & 1);
  cnt1 += (b >> 1 & 1);
  if (sz == 2) {
    if (cnt1 % 2) return true;
    return false;
  }
  int cnt2 = 0;
  cnt2 += (a >> 1 & 1);
  cnt2 += (a >> 2 & 1);
  cnt2 += (b >> 1 & 1);
  cnt2 += (b >> 2 & 1);
  if (cnt1 % 2 and cnt2 % 2) return true;
  return false;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  int grid[n][m];
  for (long long i = 0; i < n; i++) {
    string s;
    cin >> s;
    for (long long j = 0; j < m; j++) {
      if (s[j] == '1')
        grid[i][j] = 1;
      else
        grid[i][j] = 0;
    }
  }
  if (n >= 4) {
    cout << -1 << endl;
    return;
  }
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  long long dp[m + 10][1 << n];
  for (long long i = 0; i < 1 << n; i++) {
    dp[0][i] = 0;
    for (long long j = 0; j < n; j++) dp[0][i] += (grid[j][0]) ^ (i >> j & 1);
  }
  for (long long k = 1; k <= m - 1; k++) {
    for (long long i = 0; i < 1 << n; i++) {
      dp[k][i] = 1000000000;
      long long cost = 0;
      for (long long j = 0; j < n; j++) cost += (grid[j][k]) ^ (i >> j & 1);
      for (long long j = 0; j < 1 << n; j++) {
        if (compatible(i, j, n)) {
          dp[k][i] = min(dp[k][i], dp[k - 1][j] + cost);
        }
      }
    }
  }
  long long res = 1000000000;
  for (long long i = 0; i < 1 << n; i++) res = min(res, dp[m - 1][i]);
  cout << res << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
}
