#include <bits/stdc++.h>
using namespace std;
const int NMAX = 1e6;
const int INF = 1e9;
int N, M;
string a[NMAX + 2], b[NMAX + 2];
int dp[NMAX + 2][2][2];
int dp2[NMAX + 2][2][2][2];
void Solve2() {
  for (int i = 0; i < M; i++)
    for (int l1 = 0; l1 < 2; l1++)
      for (int l2 = 0; l2 < 2; l2++) dp[i][l1][l2] = INF;
  for (char l1 = '0'; l1 < '2'; l1++)
    for (char l2 = '0'; l2 < '2'; l2++)
      dp[0][l1 - '0'][l2 - '0'] = (a[0][0] != l1) + (a[1][0] != l2);
  for (int i = 1; i < M; i++) {
    for (char l1 = '0'; l1 < '2'; l1++)
      for (char l2 = '0'; l2 < '2'; l2++) {
        int pret = (a[0][i] != l1) + (a[1][i] != l2);
        int ones = (l1 == '1') + (l2 == '1');
        for (int pre1 = 0; pre1 < 2; pre1++)
          for (int pre2 = 0; pre2 < 2; pre2++) {
            int totalOnes = ones + (pre1 == 1) + (pre2 == 1);
            if (totalOnes % 2 == 1)
              dp[i][l1 - '0'][l2 - '0'] =
                  min(dp[i][l1 - '0'][l2 - '0'], pret + dp[i - 1][pre1][pre2]);
          }
      }
  }
  int ans = INF;
  for (int l1 = 0; l1 < 2; l1++)
    for (int l2 = 0; l2 < 2; l2++) ans = min(ans, dp[M - 1][l1][l2]);
  if (ans == INF)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
}
void Solve3() {
  for (int i = 0; i < M; i++)
    for (int l1 = 0; l1 < 2; l1++)
      for (int l2 = 0; l2 < 2; l2++)
        for (int l3 = 0; l3 < 2; l3++) dp2[i][l1][l2][l3] = INF;
  for (char l1 = '0'; l1 < '2'; l1++)
    for (char l2 = '0'; l2 < '2'; l2++)
      for (char l3 = '0'; l3 < '2'; l3++)
        dp2[0][l1 - '0'][l2 - '0'][l3 - '0'] =
            (a[0][0] != l1) + (a[1][0] != l2) + (a[2][0] != l3);
  for (int i = 1; i < M; i++) {
    for (char l1 = '0'; l1 < '2'; l1++)
      for (char l2 = '0'; l2 < '2'; l2++)
        for (char l3 = '0'; l3 < '2'; l3++) {
          int pret = (a[0][i] != l1) + (a[1][i] != l2) + (a[2][i] != l3);
          for (int pre1 = 0; pre1 < 2; pre1++)
            for (int pre2 = 0; pre2 < 2; pre2++)
              for (int pre3 = 0; pre3 < 2; pre3++) {
                bool ok = true;
                int ones1 =
                    (l1 == '1') + (l2 == '1') + (pre1 == 1) + (pre2 == 1);
                if (ones1 % 2 == 0) ok = false;
                int ones2 =
                    (l3 == '1') + (l2 == '1') + (pre3 == 1) + (pre2 == 1);
                if (ones2 % 2 == 0) ok = false;
                if (ok)
                  dp2[i][l1 - '0'][l2 - '0'][l3 - '0'] =
                      min(dp2[i][l1 - '0'][l2 - '0'][l3 - '0'],
                          pret + dp2[i - 1][pre1][pre2][pre3]);
              }
        }
  }
  int ans = INF;
  for (int l1 = 0; l1 < 2; l1++)
    for (int l2 = 0; l2 < 2; l2++)
      for (int l3 = 0; l3 < 2; l3++) ans = min(ans, dp2[M - 1][l1][l2][l3]);
  if (ans == INF)
    cout << -1 << '\n';
  else
    cout << ans << '\n';
}
int main() {
  cin >> N >> M;
  if (N >= 4 && M >= 4) {
    cout << -1 << '\n';
    return 0;
  }
  if (N < 2 || M < 2) {
    cout << 0 << '\n';
    return 0;
  }
  for (int i = 0; i < N; i++) cin >> b[i];
  if (N > 3) {
    for (int j = 0; j < M; j++)
      for (int i = 0; i < N; i++) a[j] += b[i][j];
  } else {
    for (int i = 0; i < N; i++) a[i] = b[i];
  }
  if (N == 2)
    Solve2();
  else
    Solve3();
  return 0;
}
