#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  char s[4][500000];
  cin >> n >> m;
  if (n >= 4) {
    cout << -1;
    return 0;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) cin >> s[i][j];
  if (n >= 4) {
    cout << -1;
    return 0;
  }
  if (n == 1) {
    cout << 0;
    return 0;
  }
  if (n == 2) {
    int ans0 = 0, ans1 = 0;
    for (int i = 1; i <= m; ++i) {
      int s0 = ((s[1][i] == '1') + (s[2][i] == '1')) % 2;
      if (i & 1)
        ans0 += (s0 != 0), ans1 += (s0 != 1);
      else
        ans0 += (s0 != 1), ans1 += (s0 != 0);
    }
    cout << min(ans0, ans1);
  } else if (n == 3) {
    int ans00 = 0, ans01 = 0, ans10 = 0, ans11 = 0;
    for (int i = 1; i <= m; ++i) {
      int s0 = ((s[1][i] == '1') + (s[2][i] == '1')) % 2,
          s1 = ((s[2][i] == '1') + (s[3][i] == '1')) % 2;
      if (i & 1) {
        ans00 += ((s0 != 0) || (s1 != 0));
        ans01 += ((s0 != 0) || (s1 != 1));
        ans10 += ((s0 != 1) || (s1 != 0));
        ans11 += ((s0 != 1) || (s1 != 1));
      } else {
        ans00 += ((s0 != 1) || (s1 != 1));
        ans01 += ((s0 != 1) || (s1 != 0));
        ans10 += ((s0 != 0) || (s1 != 1));
        ans11 += ((s0 != 0) || (s1 != 0));
      }
    }
    cout << min({ans00, ans01, ans10, ans11});
  }
  return 0;
}
