#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, m;
  cin >> n >> m;
  int a[n][m];
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> c;
      a[i][j] = c - '0';
    }
  }
  if (n >= 4) {
    cout << -1 << '\n';
    return;
  }
  if (n == 1) {
    cout << 0 << '\n';
  } else if (n == 2) {
    int a1 = 0, b = 0;
    for (int j = 0; j < m; j++) {
      if ((a[0][j] + a[1][j]) % 2 == j % 2)
        a1++;
      else
        b++;
    }
    cout << min(a1, b) << '\n';
  } else {
    int globmin = 10000000;
    int x, y, z;
    for (int i = 0; i < 4; i++) {
      x = 0;
      for (int j = 0; j < m; j++) {
        if ((i == 0 && j % 2 == 0) || (i == 2 && j % 2 == 1)) {
          y = (a[0][j] + a[1][j]) % 2;
          z = (a[1][j] + a[2][j]) % 2;
          if (y != 0 || z != 1) x++;
        } else if ((i == 1 && j % 2 == 0) || (i == 3 && j % 2 == 1)) {
          y = (a[0][j] + a[1][j]) % 2;
          z = (a[1][j] + a[2][j]) % 2;
          if (y != 1 || z != 1) x++;
        } else if ((i == 2 && j % 2 == 0) || (i == 0 && j % 2 == 1)) {
          y = (a[0][j] + a[1][j]) % 2;
          z = (a[1][j] + a[2][j]) % 2;
          if (y != 1 || z != 0) x++;
        } else {
          y = (a[0][j] + a[1][j]) % 2;
          z = (a[1][j] + a[2][j]) % 2;
          if (y != 0 || z != 0) x++;
        }
      }
      globmin = min(globmin, x);
    }
    cout << globmin << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  while (t--) solve();
  return 0;
}
