#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
void solve() {
  int n, m;
  cin >> n >> m;
  vector<vector<char>> mat(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) cin >> mat[i][j];
  }
  if (n == 1) {
    cout << 0 << '\n';
    return;
  }
  if (n >= 4) {
    cout << -1 << '\n';
    return;
  }
  int w = 1 << n;
  vector<vector<int>> diff(w, vector<int>(w));
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < w; ++j) {
      diff[i][j] = __builtin_popcount(i ^ j);
    }
  }
  vector<vector<bool>> valid(w, vector<bool>(w, 0));
  for (int i = 0; i < w; ++i) {
    for (int j = 0; j < w; ++j) {
      if (n == 2) {
        int c = 0;
        for (int k = 0; k < n; ++k) {
          if ((1 << k) & i) ++c;
          if ((1 << k) & j) ++c;
        }
        if (c & 1) valid[i][j] = 1;
      } else {
        int c[3] = {0, 0, 0};
        for (int k = 0; k < n; ++k) {
          if ((1 << k) & i) ++c[k];
          if ((1 << k) & j) ++c[k];
        }
        if (((c[0] + c[1]) & 1) && ((c[1] + c[2]) & 1)) valid[i][j] = 1;
      }
    }
  }
  vector<int> val(m);
  for (int i = 0; i < m; ++i) {
    int sum = 0;
    for (int j = 0; j < n; ++j) {
      if (mat[j][i] == '1') sum += 1 << j;
    }
    val[i] = sum;
  }
  vector<vector<int>> dp(m, vector<int>(w, INF));
  for (int i = 0; i < w; ++i) {
    dp[0][i] = diff[val[0]][i];
  }
  for (int i = 1; i < m; ++i) {
    for (int cmask = 0; cmask < w; ++cmask) {
      for (int pmask = 0; pmask < w; ++pmask) {
        if (!valid[pmask][cmask]) continue;
        dp[i][cmask] =
            min(dp[i][cmask], dp[i - 1][pmask] + diff[val[i]][cmask]);
      }
    }
  }
  int res = INF;
  for (int i = 0; i < w; ++i) {
    res = min(res, dp[m - 1][i]);
  }
  cout << res << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  solve();
  return 0;
}
