#include <bits/stdc++.h>
using namespace std;
int cnt(int n) {
  int res = 0;
  while (n > 0) {
    res += n % 2;
    n = n / 2;
  }
  return res;
}
int change(int a, int b) { return cnt(a ^ b); }
int bin2num(string s) {
  int res = 0;
  for (char c : s) {
    res = res * 2 + (c == '1');
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<string> ss(n);
  for (int i = 0; i < n; i++) cin >> ss[i];
  if (min(n, m) >= 4) {
    cout << -1 << endl;
    return 0;
  }
  if (m > n) {
    vector<string> s(m);
    for (int i = 0; i < m; i++) {
      string cur = "";
      for (int j = 0; j < n; j++) {
        cur += ss[j][i];
      }
      s[i] = cur;
    }
    ss = s;
    swap(n, m);
  }
  if (m == 1) {
    cout << 0 << endl;
    return 0;
  }
  int L = 8;
  if (m == 2) L = 4;
  vector<int> start(L);
  int s0 = bin2num(ss[0]);
  for (int i = 0; i < L; i++) {
    start[i] = change(s0, i);
  }
  vector<vector<int>> xmap(L);
  for (int i = 0; i < L; i++) {
    for (int j = 0; j < L; j++) {
      if ((cnt((i % 4) ^ (j % 4)) == 1) &&
          (m == 2 || cnt((i / 2) ^ (j / 2)) == 1)) {
        xmap[i].push_back(j);
      }
    }
  }
  for (int i = 1; i < n; i++) {
    int sc = bin2num(ss[i]);
    vector<int> temp(L);
    for (int j = 0; j < L; j++) {
      int mv = INT_MAX;
      for (int k : xmap[j]) mv = min(mv, start[k]);
      temp[j] = change(sc, j) + mv;
    }
    start = temp;
  }
  int res = INT_MAX;
  for (int i : start) {
    res = min(i, res);
  }
  cout << res << endl;
  return 0;
  return 0;
}
