#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  std::vector<string> v(n);
  for (long long int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (n > 3) {
    cout << "-1\n";
    return 0;
  }
  if (n == 1) {
    cout << "0\n";
    return 0;
  }
  std::vector<std::vector<int>> possible(1 << n);
  for (long long int i = 0; i < (1 << n); i++) {
    for (long long int j = 0; j < (1 << n); j++) {
      bool poss = true;
      for (long long int k = 0; k < n - 1; k++) {
        bool chk = 0;
        chk = chk ^ (bool)((1 << k) & j);
        chk = chk ^ (bool)(1 << (k + 1) & j);
        chk = chk ^ (bool)((1 << k) & i);
        chk = chk ^ (bool)(1 << (k + 1) & i);
        if (!chk) {
          poss = false;
          break;
        }
      }
      if (poss) possible[i].push_back(j);
    }
  }
  int dp[(1 << n)][m];
  for (long long int i = 0; i < 1 << n; i++) {
    for (long long int j = 0; j < m; j++) {
      dp[i][j] = INT_MAX;
    }
  }
  for (long long int i = 0; i < 1 << n; i++) {
    int cur = 0;
    for (long long int k = 0; k < n; k++) {
      cur += (bool)(v[k][0] - '0') ^ (bool)(i & 1 << k);
    }
    dp[i][0] = cur;
  }
  for (long long int i = 0; i < m - 1; i++) {
    for (long long int j = 0; j < 1 << n; j++) {
      int cur = 0;
      for (long long int k = 0; k < n; k++) {
        cur += (bool)(v[k][i + 1] - '0') ^ (bool)(j & 1 << k);
      }
      for (auto x : possible[j]) {
        dp[j][i + 1] = min(dp[x][i] + cur, dp[j][i + 1]);
      }
    }
  }
  int ans = INT_MAX;
  for (long long int i = 0; i < 1 << n; i++) {
    ans = min(ans, dp[i][m - 1]);
  }
  cout << ans << "\n";
}
