#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return (b == 0LL ? a : gcd(b, a % b));
}
long double dist(long double x, long double arayikhalatyan, long double x2,
                 long double y2) {
  return sqrt((x - x2) * (x - x2) +
              (arayikhalatyan - y2) * (arayikhalatyan - y2));
}
long long int S(long long int a) { return (a * (a + 1LL)) / 2; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
char vow[] = {'a', 'e', 'i', 'o', 'u'};
int dx[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};
int dy[] = {0, 0, 1, -1, -1, 1, -1, 1, 0};
const int N = 1e6 + 30;
const long long int mod = 998244353;
const long double pi = acos(-1);
const int T = 238;
const long double e = 1e-13;
long long int bp(long long int a, long long int b = mod - 2LL) {
  long long int ret = 1;
  while (b) {
    if (b & 1) ret *= a, ret %= mod;
    a *= a;
    a %= mod;
    b >>= 1;
  }
  return ret;
}
ostream& operator<<(ostream& c, pair<int, int> a) {
  c << a.first << " " << a.second;
  return c;
}
struct pt {
  long long int x, y;
  int ind;
  int bl;
  bool operator==(const pt& a) { return ind == a.ind; }
  bool operator<(const pt& b) {
    if (x == b.x) return y < b.y;
    return x < b.x;
  }
};
int n, m;
int a[4][N], dp[N][10];
int d(int x, int i1) {
  int ret = 0;
  for (int i = 0; i < 3; i++) {
    if (x & (1 << i))
      ret += 1 - a[i][i1];
    else
      ret += a[i][i1];
  }
  return ret;
}
bool stg(int a, int b) {
  int c[3], d[3];
  for (int i = 0; i < 3; i++) {
    if (a & (1 << i))
      c[i] = 1;
    else
      c[i] = 0;
    if (b & (1 << i))
      d[i] = 1;
    else
      d[i] = 0;
  }
  if ((c[0] + c[1] + d[0] + d[1]) % 2 == 0) return false;
  if ((c[1] + c[2] + d[1] + d[2]) % 2 == 0) return false;
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  cin >> n >> m;
  if (n >= 4 && m >= 4) return cout << -1 << endl, 0;
  if (n <= m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        char A;
        cin >> A;
        a[i][j] = A - '0';
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        char A;
        cin >> A;
        a[j][i] = A - '0';
      }
    }
    swap(n, m);
  }
  if (n == 1) return cout << 0 << endl, 0;
  if (n == 2) {
    dp[0][0] = dp[0][1] = 1;
    dp[0][(a[0][0] + a[1][0]) % 2]--;
    for (int i = 1; i < m; i++) {
      int sm = (a[0][i] + a[1][i]) % 2;
      dp[i][sm] = dp[i - 1][1 - sm];
      dp[i][1 - sm] = 1 + dp[i - 1][sm];
    }
    cout << min(dp[m - 1][0], dp[m - 1][1]) << endl;
    return 0;
  } else {
    for (int i = 0; i < 8; i++) {
      dp[0][i] = d(i, 0);
    }
    for (int i = 1; i < m; i++) {
      for (int k = 0; k < 8; k++) {
        dp[i][k] = mod;
      }
      for (int j = 0; j < 8; j++) {
        for (int k = 0; k < 8; k++) {
          if (stg(j, k)) {
            dp[i][k] = min(dp[i][k], dp[i - 1][j] + d(k, i));
          }
        }
      }
    }
    int pat = mod;
    for (int i = 0; i < 8; i++) {
      pat = min(pat, dp[m - 1][i]);
    }
    cout << pat << endl;
  }
  return 0;
}
