#include <bits/stdc++.h>
using namespace std;
vector<int> a[5];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  if (n >= 4 && m >= 4) {
    cout << -1;
    return 0;
  }
  if (n == 1 || m == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      char c;
      cin >> c;
      if (n <= m)
        a[i].push_back(c - '0');
      else
        a[j].push_back(c - '0');
    }
  }
  if (n > m) swap(n, m);
  if (n == 2) {
    int cnt1 = 0;
    for (int i = 0; i < m; i++) {
      if (i % 2)
        cnt1 += (a[0][i] ^ a[1][i]);
      else
        cnt1 += !(a[0][i] ^ a[1][i]);
    }
    int cnt2 = 0;
    for (int i = 0; i < m; i++) {
      if (i % 2)
        cnt2 += !(a[0][i] ^ a[1][i]);
      else
        cnt2 += (a[0][i] ^ a[1][i]);
    }
    cout << min(cnt1, cnt2);
    return 0;
  }
  int cnt1 = 0;
  for (int i = 0; i < m; i++) {
    if (i % 2)
      cnt1 += !(a[0][i] == a[1][i] && a[1][i] != a[2][i]);
    else
      cnt1 += !(a[0][i] != a[1][i] && a[1][i] == a[2][i]);
  }
  int cnt2 = 0;
  for (int i = 0; i < m; i++) {
    if (i % 2)
      cnt2 += !(a[0][i] != a[1][i] && a[1][i] == a[2][i]);
    else
      cnt2 += !(a[0][i] == a[1][i] && a[1][i] != a[2][i]);
  }
  int cnt3 = 0;
  for (int i = 0; i < m; i++) {
    if (i % 2)
      cnt3 += !(a[0][i] == a[2][i] && a[0][i] != a[1][i]);
    else
      cnt3 += !(a[0][i] == a[2][i] && a[0][i] == a[1][i]);
  }
  int cnt4 = 0;
  for (int i = 0; i < m; i++) {
    if (i % 2)
      cnt4 += !(a[0][i] == a[2][i] && a[0][i] == a[1][i]);
    else
      cnt4 += !(a[0][i] == a[2][i] && a[0][i] != a[1][i]);
  }
  cout << min({cnt1, cnt2, cnt3, cnt4});
  return 0;
}
