#include <bits/stdc++.h>
using namespace std;
const long long pii = 3.1415926535897;
const long long oo = 1e18 + 13;
const int N = 1e6 + 13;
const int M = 1e3 + 13;
const double eps = 0.0000001;
long long n, m, f[N][10], a[N];
bool check_bit_pos(long long n, long long pos) { return (n | (1 << pos)) == n; }
bool ok(long long t) {
  for (int i = 1; i < min(n, m); ++i)
    if (check_bit_pos(t, i) == check_bit_pos(t, i - 1)) return 0;
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  if (n == 1 || m == 1) return cout << 0, 0;
  if (n >= 4 && m >= 4) return cout << -1, 0;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) {
      char x;
      cin >> x;
      if (x == '1') {
        if (n <= m)
          a[j + 1] |= (1 << i);
        else
          a[i + 1] |= (1 << j);
      }
    }
  memset(f, 0x3f, sizeof f);
  for (int i = 0; i <= 8; ++i) f[0][i] = 0;
  for (int i = 1; i <= max(n, m); ++i)
    for (int state = 0; state < (1 << min(n, m)); ++state)
      for (int msk = 0; msk < (1 << min(n, m)); ++msk)
        if (ok(msk ^ state))
          f[i][state] = min(f[i - 1][msk] + __builtin_popcountll(a[i] ^ state),
                            f[i][state]);
  long long res = oo;
  for (int i = 0; i < (1 << min(n, m)); ++i) res = min(res, f[max(n, m)][i]);
  cout << res;
}
