#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
void umin(T& x, const U& y) {
  x = min(x, (T)y);
}
template <class T, class U>
void umax(T& x, const U& y) {
  x = max(x, (T)y);
}
template <class T, class U>
void init(vector<T>& v, U x, size_t n) {
  v = vector<T>(n, x);
}
template <class T, class U>
void init(vector<T>& v, U x, size_t n, size_t m...) {
  v = vector<T>(n);
  for (auto& a : v) init(a, x, m);
}
int bc(int x) {
  int ret = 0;
  while (x) {
    ret += x & 1;
    x /= 2;
  }
  return ret;
}
int cost(vector<string> a, int x0, int x1) {
  int ret = 0;
  int m = a[0].size();
  for (int j = 0; j < m; j++) {
    int s0 = a[0][j] - '0' + a[1][j] - '0';
    int s1 = a[1][j] - '0' + a[2][j] - '0';
    int u = (a[0][j] - '0') * 4 + (a[1][j] - '0') * 2 + (a[2][j] - '0');
    if (s0 % 2 != x0 || s1 % 2 != x1) {
      int dr = 3;
      for (int v = 0; v < 8; v++) {
        if ((v / 4 + v / 2 % 2) % 2 == x0 && (v / 2 % 2 + v % 2) % 2 == x1) {
          umin(dr, bc(v ^ u));
        }
      }
      ret += dr;
    }
    x0 ^= 1;
    x1 ^= 1;
  }
  return ret;
}
int main(int argc, char** argv) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(15);
  if (argc == 2 && atoi(argv[1]) == 123456789)
    freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);
  int T = 1;
  for (int tt = 1; tt <= T; tt++) {
    int n, m;
    cin >> n >> m;
    vector<string> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    if (n >= 4 && m >= 4)
      cout << -1 << endl;
    else if (n == 3) {
      int sol = n * m;
      for (int ii = 0; ii < 2; ii++) {
        for (int jj = 0; jj < 2; jj++) {
          umin(sol, cost(a, ii, jj));
        }
      }
      cout << sol << endl;
    } else if (n == 2) {
      int x0 = 0, x1 = 0;
      for (int j = 0; j < m; j++) {
        int s = a[0][j] - '0' + a[1][j] - '0';
        if (s % 2 == j % 2)
          x1++;
        else
          x0++;
      }
      cout << min(x0, x1) << endl;
    } else
      cout << 0 << endl;
  }
  if (argc == 2 && atoi(argv[1]) == 123456789)
    cout << clock() * 1.0 / CLOCKS_PER_SEC << " sec\n";
  return 0;
}
