#include <bits/stdc++.h>
using namespace std;
int n, m;
string str[1000100];
bool flag;
int v[1000100];
bool a[3][1000100];
int dp[2][8];
int c[8], c4[4][4], c8[8][8];
int M;
int ans;
int main() {
  c[0] = 0;
  for (int i = 1; i < 8; i++) c[i] = c[i >> 1] + (i & 1);
  for (int i = 0; i < 4; i++)
    for (int j = 0; j < 4; j++) {
      c4[i][j] = c[i ^ j];
    }
  for (int i = 0; i < 8; i++)
    for (int j = 0; j < 8; j++) {
      c8[i][j] = c[i ^ j];
    }
  while (~scanf("%d%d", &n, &m)) {
    if (n > m) {
      flag = true;
    } else
      flag = false;
    for (int i = 0; i < n; i++) cin >> str[i];
    if (min(n, m) > 3)
      ans = -1;
    else {
      if (flag) {
        for (int i = 0; i < n; i++)
          for (int j = 0; j < m; j++) {
            a[j][i] = str[i][j] - '0';
          }
        swap(n, m);
      } else {
        for (int i = 0; i < n; i++)
          for (int j = 0; j < m; j++) {
            a[i][j] = str[i][j] - '0';
          }
      }
      int cur, nxt;
      int tmp;
      if (n == 1)
        ans = 0;
      else if (n == 2) {
        M = n * m;
        for (int i = 0; i < m; i++) {
          v[i] = (int)a[0][i] + (int)a[1][i] * 2;
        }
        dp[0][0] = 0;
        for (int j = 1; j < 4; j++) dp[0][j] = M;
        for (int i = 0; i < m; i++) {
          cur = i & 1;
          nxt = cur ^ 1;
          for (int k = 0; k < 4; k++) dp[nxt][k] = M;
          for (int k1 = 0; k1 < 4; k1++) {
            if (dp[cur][k1] == M) continue;
            for (int k2 = 0; k2 < 4; k2++) {
              if ((c[k1 ^ k2] & 1) || (i == 0)) {
                dp[nxt][k2] = min(dp[nxt][k2], dp[cur][k1] + c4[k2][v[i]]);
              }
            }
          }
        }
        ans = M;
        cur = m & 1;
        for (int i = 0; i < 4; i++) ans = min(ans, dp[cur][i]);
      } else {
        M = n * m;
        for (int i = 0; i < m; i++) {
          v[i] = (int)a[0][i] + (int)a[1][i] * 2 + (int)a[2][i] * 4;
        }
        dp[0][0] = 0;
        for (int j = 1; j < 8; j++) dp[0][j] = M;
        for (int i = 0; i < m; i++) {
          cur = i & 1;
          nxt = cur ^ 1;
          for (int k = 0; k < 8; k++) dp[nxt][k] = M;
          for (int k1 = 0; k1 < 8; k1++) {
            if (dp[cur][k1] == M) continue;
            for (int k2 = 0; k2 < 8; k2++) {
              tmp = k1 ^ k2;
              if (((c[tmp & 3] & 1) && (c[tmp & 6] & 1)) || (i == 0)) {
                dp[nxt][k2] = min(dp[nxt][k2], dp[cur][k1] + c8[k2][v[i]]);
              }
            }
          }
        }
        ans = M;
        cur = m & 1;
        for (int i = 0; i < 8; i++) ans = min(dp[cur][i], ans);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
