#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, ans;
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  vector<string> s(n);
  vector<int> a(m);
  for (int i = 0; i < n; i++) cin >> s[i];
  if (n > 3)
    ans = -1;
  else if (n == 1)
    ans = 0;
  else if (n == 2) {
    ans = n * m;
    int tmp = 0;
    for (int i = 0; i < m; i++)
      if ((i % 2) ^ (s[0][i] != s[1][i])) tmp++;
    ans = min(ans, tmp);
    tmp = 0;
    for (int i = 0; i < m; i++)
      if ((i % 2) ^ (s[0][i] == s[1][i])) tmp++;
    ans = min(ans, tmp);
  } else {
    for (int i = 0; i < m; i++)
      a[i] = (s[0][i] - '0') * 1 + (s[1][i] - '0') * 2 + (s[2][i] - '0') * 4;
    pair<int, int> p[2][2] = {{{0, 7}, {2, 5}}, {{3, 4}, {1, 6}}};
    ans = n * m;
    for (int i = 0; i < 2; i++) {
      for (int j = 0; j < 2; j++) {
        int tmp = 0;
        int z = j;
        for (int k = 0; k < m; k++) {
          tmp += min(__builtin_popcount(a[k] ^ p[i][z].first),
                     __builtin_popcount(a[k] ^ p[i][z].second));
          z ^= 1;
        }
        ans = min(ans, tmp);
      }
    }
  }
  cout << ans << endl;
  return 0;
}
