#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e6 + 6;
int m, memo[MAX][8], a[MAX], stop;
bool check(int prevMask, int curMask) {
  int row0 = (prevMask & 1) + (curMask & 1);
  int row1 = ((prevMask & 2) == 2) + ((curMask & 2) == 2);
  int row2 = ((prevMask & 4) == 4) + ((curMask & 4) == 4);
  if ((row0 + row1) % 2 == 0) {
    return 0;
  }
  if (stop == 7 && (row2 + row1) % 2 == 0) {
    return 0;
  }
  return 1;
}
int dp(int i, int prevMask) {
  if (i == m) {
    return 0;
  }
  int& ret = memo[i][prevMask];
  if (ret != -1) {
    return ret;
  }
  ret = MAX;
  for (int j = 0; j <= stop; ++j) {
    if (!i || check(prevMask, j)) {
      int cost = __builtin_popcount(a[i] ^ j);
      ret = min(ret, cost + dp(i + 1, j));
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n >> m;
  if (n >= 4 && m >= 4) {
    cout << -1;
    return 0;
  }
  vector<vector<char> > v(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> v[i][j];
    }
  }
  if (n > m) {
    vector<vector<char> > temp(m, vector<char>(m));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        temp[j][i] = v[i][j];
      }
    }
    v = temp;
    swap(n, m);
  }
  for (int j = 0; j < m; ++j) {
    for (int i = 0; i < n; ++i) {
      if (v[i][j] == '1') {
        a[j] |= (1 << i);
      }
    }
  }
  if (n == 1) {
    cout << 0;
    return 0;
  } else if (n == 2) {
    stop = 3;
  } else {
    stop = 7;
  }
  memset(memo, -1, sizeof memo);
  cout << dp(0, 0);
  return 0;
}
