#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int mod = 1e9 + 7;
int n, m;
string s[1000006];
int dp[20][1000005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> s[i];
  if (min(n, m) >= 4) {
    puts("-1");
  } else {
    int x = min(n, m);
    int y = max(n, m);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) s[i][j] -= '0';
    }
    if (x == 1) {
      puts("0");
      return 0;
    }
    if (x == 2) {
      int a, b;
      if (n == x) {
        a = s[0][0], b = s[1][0];
      } else {
        a = s[0][0], b = s[0][1];
      }
      dp[0][0] = a + b;
      dp[1][0] = (a != 0) + (b != 1);
      dp[2][0] = (a != 1) + (b != 0);
      dp[3][0] = (a != 1) + (b != 1);
      for (int i = 1; i < y; i++) {
        if (n == x) {
          a = s[0][i], b = s[1][i];
        } else {
          a = s[i][0], b = s[i][1];
        }
        dp[0][i] = a + b;
        dp[1][i] = (a != 0) + (b != 1);
        dp[2][i] = (a != 1) + (b != 0);
        dp[3][i] = (a != 1) + (b != 1);
        dp[0][i] += min(dp[1][i - 1], dp[2][i - 1]);
        dp[1][i] += min(dp[0][i - 1], dp[3][i - 1]);
        dp[2][i] += min(dp[0][i - 1], dp[3][i - 1]);
        dp[3][i] += min(dp[1][i - 1], dp[2][i - 1]);
      }
      int ans = min(dp[0][y - 1], dp[1][y - 1]);
      ans = min(ans, min(dp[2][y - 1], dp[3][y - 1]));
      printf("%d\n", ans);
    } else {
      int a[5];
      if (n == x) {
        a[0] = s[0][0], a[1] = s[1][0], a[2] = s[2][0];
      } else {
        a[0] = s[0][0], a[1] = s[0][1], a[2] = s[0][2];
      }
      for (int j = 0; j <= 7; j++) {
        int p = j;
        dp[j][0] = 0;
        for (int k = 0; k < 3; k++) {
          dp[j][0] += (a[k] != (p & 1));
          p /= 2;
        }
      }
      for (int i = 1; i < y; i++) {
        if (n == x) {
          a[0] = s[0][i], a[1] = s[1][i], a[2] = s[2][i];
        } else {
          a[0] = s[i][0], a[1] = s[i][1], a[2] = s[i][2];
        }
        for (int j = 0; j <= 7; j++) {
          int c[3][2];
          int p = j;
          dp[j][i] = 0;
          for (int k = 0; k < 3; k++) {
            dp[j][i] += (a[k] != (p & 1));
            c[k][0] = p & 1;
            p /= 2;
          }
          int dt = 0x3f3f3f3f;
          for (int k = 0; k <= 7; k++) {
            p = k;
            for (int l = 0; l < 3; l++) {
              c[l][1] = p & 1;
              p >>= 1;
            }
            if ((c[0][0] + c[1][0] + c[0][1] + c[1][1]) % 2) {
              if ((c[1][0] + c[2][0] + c[1][1] + c[2][1]) % 2) {
                dt = min(dt, dp[k][i - 1]);
              }
            }
          }
          dp[j][i] += dt;
        }
      }
      int ans = 0x3f3f3f3f;
      for (int i = 0; i <= 7; i++) {
        ans = min(ans, dp[i][y - 1]);
      }
      printf("%d\n", ans);
    }
  }
}
