#include <bits/stdc++.h>
using namespace std;
const int mxN = 1e6 + 5, mod = 1e9 + 7;
int n, m;
bool good[(1 << 3)][(1 << 3)];
int main() {
  ios ::sync_with_stdio(false), cin.tie(nullptr);
  cin >> n >> m;
  if (min(n, m) > 3) {
    cout << -1;
    return 0;
  }
  vector<vector<char> > a(n, vector<char>(m));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) cin >> a[i][j];
  }
  if (n <= m) {
    for (int mask1 = 0; mask1 < (1 << n); ++mask1) {
      for (int mask2 = 0; mask2 < (1 << n); ++mask2) {
        bool ok = 1;
        for (int i = 0; i < n - 1; ++i) {
          int cnt = ((mask1 >> i) & 1) + ((mask1 >> (i + 1)) & 1);
          cnt += ((mask2 >> i) & 1) + ((mask2 >> (i + 1)) & 1);
          ok &= (cnt & 1);
        }
        good[mask1][mask2] = ok;
      }
    }
    vector<vector<int> > dp(m, vector<int>((1 << n), mod));
    for (int mask = 0; mask < (1 << n); ++mask) {
      int c = 0;
      for (int i = 0; i < n; ++i) {
        if ((mask >> i) & 1)
          c += (a[i][0] != '1');
        else
          c += (a[i][0] != '0');
      }
      dp[0][mask] = c;
    }
    for (int i = 1; i < m; ++i) {
      for (int mask1 = 0; mask1 < (1 << n); ++mask1) {
        int c = 0;
        for (int j = 0; j < n; ++j) {
          if ((mask1 >> j) & 1)
            c += (a[j][i] != '1');
          else
            c += (a[j][i] != '0');
        }
        for (int mask2 = 0; mask2 < (1 << n); ++mask2) {
          if (good[mask1][mask2])
            dp[i][mask1] = min(dp[i][mask1], dp[i - 1][mask2] + c);
        }
      }
    }
    int ans = mod;
    for (int mask = 0; mask < (1 << n); ++mask) ans = min(ans, dp[m - 1][mask]);
    cout << ans;
  } else {
    for (int mask1 = 0; mask1 < (1 << m); ++mask1) {
      for (int mask2 = 0; mask2 < (1 << m); ++mask2) {
        bool ok = 1;
        for (int i = 0; i < m - 1; ++i) {
          int cnt = ((mask1 >> i) & 1) + ((mask1 >> (i + 1)) & 1);
          cnt += ((mask2 >> i) & 1) + ((mask2 >> (i + 1)) & 1);
          ok &= (cnt & 1);
        }
        good[mask1][mask2] = ok;
      }
    }
    vector<vector<int> > dp(n, vector<int>((1 << m), mod));
    for (int mask = 0; mask < (1 << m); ++mask) {
      int c = 0;
      for (int i = 0; i < m; ++i) {
        if ((mask >> i) & 1)
          c += (a[0][i] != '1');
        else
          c += (a[0][i] != '0');
      }
      dp[0][mask] = c;
    }
    for (int i = 1; i < n; ++i) {
      for (int mask1 = 0; mask1 < (1 << m); ++mask1) {
        int c = 0;
        for (int j = 0; j < m; ++j) {
          if ((mask1 >> j) & 1)
            c += (a[i][j] != '1');
          else
            c += (a[i][j] != '0');
        }
        for (int mask2 = 0; mask2 < (1 << m); ++mask2) {
          if (good[mask1][mask2])
            dp[i][mask1] = min(dp[i][mask1], dp[i - 1][mask2] + c);
        }
      }
    }
    int ans = mod;
    for (int mask = 0; mask < (1 << m); ++mask) ans = min(ans, dp[n - 1][mask]);
    cout << ans;
  }
  return 0;
}
