#include <bits/stdc++.h>
using namespace std;
ifstream fin("AAtest.in.txt");
int n, m, mi, par, a1, a2;
char c[4][1000005];
int b[4][1000005], b1[4][1000005];
void rek(int l) {
  if (l < n) {
    for (int i = 0; i <= 1; i++) {
      if (i != c[l][0] - '0') par++;
      if (i) {
        if (l == 0)
          b[0][0] = b[0][0] xor 1;
        else if (l == 1)
          b[0][0] = b[0][0] xor 1, b[1][0] = b[1][0] xor 1;
        else
          b[1][0] = b[1][0] xor 1;
      }
      rek(l + 1);
      if (i) {
        if (l == 0)
          b[0][0] = b[0][0] xor 1;
        else if (l == 1)
          b[0][0] = b[0][0] xor 1, b[1][0] = b[1][0] xor 1;
        else
          b[1][0] = b[1][0] xor 1;
      }
      if (i != c[l][0] - '0') par--;
    }
  } else {
    for (int i = 0; i < m - 1; i++) b1[0][i] = b[0][i], b1[1][i] = b[1][i];
    int par1 = par;
    for (int i = 0; i < m - 1; i++) {
      if (c[0][i + 1] != '0')
        a1 = 1, a2 = 0;
      else
        a2 = 1, a1 = 0;
      b1[0][i + 1] = b1[0][i] xor 1;
      b1[1][i + 1] = b1[1][i] xor 1;
      for (int j = 1; j <= n - 1; j++) {
        if (!b1[j - 1][i]) {
          if (c[j][i + 1] == '0')
            a1++;
          else
            a2++;
          b1[j][i] = b1[j][i] xor 1;
        } else if (c[j][i + 1] != '0')
          a1++;
        else
          a2++;
      }
      par1 += min(a1, a2);
    }
    mi = min(mi, par1);
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  cin >> n >> m;
  if (n >= 4) {
    cout << -1;
    return 0;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> c[i][j];
  mi = 1e9;
  rek(0);
  cout << mi;
}
