#include <bits/stdc++.h>
using namespace std;
void fastio(void);
int find_val2(char a, char b) { return (a - '0') + 2 * (b - '0'); }
int find_val3(char a, char b, char c) {
  return (a - '0') + 2 * (b - '0') + 4 * (c - '0');
}
int find_dif(int a, int b) {
  int cnt = 0;
  while (a > 0 || b > 0) {
    if ((a & 1) != (b & 1)) cnt++;
    a /= 2;
    b /= 2;
  }
  return cnt;
}
bool poss(int a, int b, int n) {
  if (n == 2) {
    int c = (a ^ b);
    if (c == 1 || c == 2) return true;
  }
  if (n == 3) {
    int c = (a ^ b);
    if (c == 2 || c == 5) return true;
  }
  return false;
}
void solve() {
  int n, m, la, lb, lc;
  cin >> n >> m;
  vector<string> st(n);
  for (la = 0; la < n; la++) cin >> st[la];
  if (n <= 1) {
    cout << 0 << endl;
    return;
  }
  if (n > 3) {
    cout << -1 << endl;
    return;
  }
  vector<vector<int>> dp((1 << n), vector<int>(m, INT_MAX));
  for (la = 0; la < m; la++) {
    int val = 0;
    if (n == 2) val = find_val2(st[0][la], st[1][la]);
    if (n == 3) val = find_val3(st[0][la], st[1][la], st[2][la]);
    if (la == 0) {
      for (lb = 0; lb < (1 << n); lb++) dp[lb][la] = find_dif(val, lb);
      continue;
    }
    for (lb = 0; lb < (1 << n); lb++) {
      for (lc = 0; lc < (1 << n); lc++) {
        if (poss(lb, lc, n))
          dp[lc][la] = min(dp[lc][la], dp[lb][la - 1] + find_dif(val, lc));
      }
    }
  }
  int ans = INT_MAX;
  for (la = 0; la < (1 << n); la++) ans = min(ans, dp[la][m - 1]);
  cout << ans << endl;
}
int main() {
  fastio();
  solve();
  return 0;
}
void fastio(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
