#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  char arr[n][m];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      arr[i][j] = c;
    }
  }
  if (n >= 4 && m >= 4) {
    cout << -1;
    return 0;
  }
  int mn = min(m, n);
  int mx = max(m, n);
  int res[min(m, n)][max(m, n)];
  if (m < n) {
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < n; j++) {
        res[i][j] = (int)(arr[j][i] - '0');
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        res[i][j] = (int)(arr[i][j] - '0');
      }
    }
  }
  int bound = 1;
  for (int i = 0; i < mn; i++) {
    bound *= 2;
  }
  int valid[bound][bound];
  for (int i = 0; i < bound; i++) {
    for (int j = 0; j < bound; j++) {
      vector<int> v1, v2;
      int first = i;
      int second = j;
      for (int l = 0; l < mn; l++) {
        v1.push_back(first % 2);
        v2.push_back(second % 2);
        first /= 2;
        second /= 2;
      }
      int done = 0;
      for (int l = 1; l < mn; l++) {
        int ones = v1[l] + v2[l] + v1[l - 1] + v2[l - 1];
        if (ones % 2 == 0) {
          done = 1;
          break;
        }
      }
      if (done == 0) {
        valid[i][j] = 1;
      } else {
        valid[i][j] = 0;
      }
    }
  }
  int dp[mx][bound];
  for (int i = 0; i < mx; i++) {
    for (int j = 0; j < bound; j++) {
      dp[i][j] = 1000008;
    }
  }
  for (int i = 0; i < bound; i++) {
    int cur = i;
    vector<int> v;
    for (int j = 0; j < mn; j++) {
      int r = cur % 2;
      v.push_back(r);
      cur /= 2;
    }
    int rr = 0;
    for (int j = 0; j < mn; j++) {
      if (res[j][0] != v[j]) {
        rr++;
      }
    }
    dp[0][i] = rr;
  }
  for (int k = 1; k < mx; k++) {
    for (int i = 0; i < bound; i++) {
      int cur = i;
      vector<int> v;
      for (int j = 0; j < mn; j++) {
        int r = cur % 2;
        v.push_back(r);
        cur /= 2;
      }
      int rr = 0;
      for (int j = 0; j < mn; j++) {
        if (res[j][k] != v[j]) {
          rr++;
        }
      }
      for (int j = 0; j < bound; j++) {
        if (valid[i][j] == 1 && dp[k - 1][j] != 1000008)
          dp[k][i] = min(dp[k][i], dp[k - 1][j] + rr);
      }
    }
  }
  int finalAns = 1000008;
  for (int i = 0; i < bound; i++) {
    finalAns = min(finalAns, dp[mx - 1][i]);
  }
  cout << finalAns;
  return 0;
}
