#include <bits/stdc++.h>
using namespace std;
long long int min(long long int a, long long int b) { return a < b ? a : b; }
long long int max(long long int a, long long int b) { return a > b ? a : b; }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t, n, k, a, b, m;
  string s;
  cin >> n >> m;
  if (n > 3 && m > 3) {
    cout << "-1" << endl;
  } else if (n == 1 || m == 1) {
    cout << 0 << endl;
  } else if (n == 2) {
    vector<vector<int>> v(n);
    for (long long int i = 0; i < n; i++) {
      cin >> s;
      for (long long int j = 0; j < m; j++) {
        v[i].push_back((int)s[j] - (int)'0');
      }
    }
    vector<vector<long long int>> dp(m);
    for (long long int i = 0; i < m; i++) {
      dp[i].assign(4, LLONG_MAX);
    }
    vector<vector<long long int>> x = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
    for (long long int i = 0; i < 4; i++) {
      dp[0][i] = abs(v[0][0] - x[i][0]) + abs(v[1][0] - x[i][1]);
    }
    vector<vector<long long int>> compat(4);
    compat[0] = {1, 2};
    compat[3] = {1, 2};
    compat[1] = {0, 3};
    compat[2] = {0, 3};
    long long int change;
    for (long long int i = 1; i < m; i++) {
      for (long long int j = 0; j < 4; j++) {
        for (auto k : compat[j]) {
          change = abs(v[0][i] - x[k][0]) + abs(v[1][i] - x[k][1]);
          dp[i][k] = min(dp[i][k], change + dp[i - 1][j]);
        }
      }
    }
    cout << min(min(dp[m - 1][0], dp[m - 1][1]),
                min(dp[m - 1][2], dp[m - 1][3]))
         << endl;
  } else if (n == 3) {
    vector<vector<int>> v(n);
    for (long long int i = 0; i < n; i++) {
      cin >> s;
      for (long long int j = 0; j < m; j++) {
        v[i].push_back((int)s[j] - (int)'0');
      }
    }
    vector<vector<long long int>> dp(m);
    for (long long int i = 0; i < m; i++) {
      dp[i].assign(8, LLONG_MAX);
    }
    vector<vector<long long int>> x = {{0, 0, 0}, {0, 0, 1}, {0, 1, 0},
                                       {0, 1, 1}, {1, 0, 0}, {1, 0, 1},
                                       {1, 1, 0}, {1, 1, 1}};
    for (long long int i = 0; i < 8; i++) {
      dp[0][i] = abs(v[0][0] - x[i][0]) + abs(v[1][0] - x[i][1]) +
                 abs(v[2][0] - x[i][2]);
    }
    vector<vector<long long int>> compat(8);
    for (long long int j = 0; j < 8; j++) {
      for (long long int k = 0; k < 8; k++) {
        if ((x[j][0] + x[j][1] + x[k][0] + x[k][1]) % 2 == 1 &&
            (x[j][1] + x[j][2] + x[k][1] + x[k][2]) % 2 == 1) {
          compat[j].push_back(k);
        }
      }
    }
    long long int change;
    for (long long int i = 1; i < m; i++) {
      for (long long int j = 0; j < 8; j++) {
        for (auto k : compat[j]) {
          change = abs(v[0][i] - x[k][0]) + abs(v[1][i] - x[k][1]) +
                   abs(v[2][i] - x[k][2]);
          dp[i][k] = min(dp[i][k], change + dp[i - 1][j]);
        }
      }
    }
    long long int ans =
        min(min(dp[m - 1][0], dp[m - 1][1]), min(dp[m - 1][2], dp[m - 1][3]));
    ans = min(ans, min(min(dp[m - 1][4], dp[m - 1][5]),
                       min(dp[m - 1][6], dp[m - 1][7])));
    cout << ans << endl;
  }
}
