#include <bits/stdc++.h>
using namespace std;
bool comp(int i, int j) { return (i > j); }
bool comp1(pair<int, pair<int, int> > i, pair<int, pair<int, int> > j) {
  if (i.first == j.first)
    return (i.second.first < j.second.first);
  else
    return (i.first > j.first);
}
long long err(long long x, long long y, long long z) {
  return (((x - y) / 2) * ((x - y) / 2) + ((x - z) / 2) * ((x - z) / 2) +
          ((y - z) / 2) * ((y - z) / 2));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  double f, g, e1, e2, pi;
  long long bb, cc, tt, dd, i, j, maxx, a0, rrr, minn, ans_l, ans_r, min_sum,
      min_pos, sum, sum1, sum2, sum3, sum4, sum5, k1, k2, kk1, kk2;
  long long y, x, u, h, a1, aa, k, n, t,
      zz = 0, kl, l, r, odn = 1, m, m0, m3, m4, xl, xr, m1, m2, yl, yr, i1, i2,
      a2, i3, nn, x1, x2, y1, y2, z11, z1, z2, z3, z4;
  short int a[500005][3];
  string s, s1, s2;
  set<int> b, c;
  char ch;
  set<long long> nk;
  l = 0;
  h = 0;
  pi = atan(1) * 4;
  ch = 0;
  bb = 0;
  aa = 0;
  a0 = 0;
  zz = 20000000000;
  k = 0;
  r = 0;
  t = 0;
  j = 0;
  zz = 0;
  k1 = 0;
  k2 = 0;
  kk1 = 0;
  kk2 = 0;
  m3 = 0;
  m4 = 0;
  sum = 0;
  {
    cin >> n >> m;
    if (n > 3 and m > 3)
      cout << "-1";
    else if (n == 1 or m == 1)
      cout << "0";
    else {
      if (n == 2) {
        cin >> s1 >> s2;
        for (i = 0; i < m; i++) {
          a[i][0] = s1[i] - '0';
          a[i][1] = s2[i] - '0';
        }
        n = m;
        m = 2;
      } else if (n == 3) {
        cin >> s1 >> s2 >> s;
        for (i = 0; i < m; i++) {
          a[i][0] = s1[i] - '0';
          a[i][1] = s2[i] - '0';
          a[i][2] = s[i] - '0';
        }
        n = m;
        m = 3;
      } else if (m == 2) {
        for (i = 0; i < n; i++) {
          cin >> s;
          a[i][0] = s[0] - '0';
          a[i][1] = s[1] - '0';
        }
      } else {
        for (i = 0; i < n; i++) {
          cin >> s;
          a[i][0] = s[0] - '0';
          a[i][1] = s[1] - '0';
          a[i][2] = s[2] - '0';
        }
      }
      m1 = 0;
      m2 = 0;
      if (m == 2) {
        for (i = 0; i < n; i++) {
          if ((a[i][0] + a[i][1]) % 2 == 0) {
            if (i % 2 == 0)
              m1++;
            else
              m2++;
          } else {
            if (i % 2 == 1)
              m1++;
            else
              m2++;
          }
        }
        if (m2 < m1) m1 = m2;
        cout << m1;
      } else {
        m1 = 0;
        m2 = 0;
        m3 = 0;
        m4 = 0;
        for (i = 0; i < n; i++) {
          x1 = (a[i][0] + a[i][1]) % 2;
          x2 = (a[i][1] + a[i][2]) % 2;
          if (i % 2 != 0) {
            x1 = 1 - x1;
            x2 = 1 - x2;
          }
          if (x1 == 0 and x2 == 0) {
            m2++;
            m3++;
            m4++;
          }
          if (x1 == 1 and x2 == 1) {
            m1++;
            m3++;
            m4++;
          }
          if (x1 == 1 and x2 == 0) {
            m2++;
            m1++;
            m4++;
          }
          if (x1 == 0 and x2 == 1) {
            m2++;
            m3++;
            m1++;
          }
        }
        if (m2 < m1) m1 = m2;
        if (m3 < m1) m1 = m3;
        if (m4 < m1) m1 = m4;
        cout << m1;
      }
    }
  }
  return 0;
}
