#include <bits/stdc++.h>
const double pi = 3.141592653589793238462643383279;
using namespace std;
const int MAX = 2000010;
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % 1000000007;
    inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007;
    finv[i] = finv[i - 1] * inv[i] % 1000000007;
  }
}
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007;
}
long long dp[1000010][(1 << 3)];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(5);
  int n, m;
  cin >> n >> m;
  vector<string> v;
  for (int i = 0; i < (int)n; ++i) {
    string s;
    cin >> s;
    v.push_back(s);
  }
  for (int j = 0; j < (int)1000010; ++j)
    for (int k = 0; k < (int)(1 << 3); ++k) dp[j][k] = INT_MAX;
  if (n >= 4 && m >= 4) {
    cout << -1 << endl;
  } else {
    if (n == 1) {
      cout << 0 << endl;
    } else if (n == 2) {
      int tmp = (v[0][0] - '0') * 2 + v[1][0] - '0';
      for (int bit = 0; bit < (1 << 2); bit++) {
        int cost = 0;
        for (int i = 0; i < 2; i++) {
          if (((bit >> i) & 1) != ((tmp >> i) & 1)) cost++;
        }
        dp[1][bit] = cost;
      }
      for (int i = 1; i < m; i++) {
        int tmp = (v[0][i] - '0') * 2 + v[1][i] - '0';
        for (int v = 0; v < (1 << 2); v++) {
          int t1 = 0;
          for (int j = 0; j < (1 << 2); j++) {
            if ((v >> j) & 1) t1++;
          }
          for (int u = 0; u < (1 << 2); u++) {
            int t2 = 0;
            for (int j = 0; j < (1 << 2); j++) {
              if ((u >> j) & 1) t2++;
            }
            if ((t1 + t2) % 2 == 0) continue;
            int cost = 0;
            for (int j = 0; j < 2; j++) {
              if (((u >> j) & 1) != ((tmp >> j) & 1)) cost++;
            }
            dp[i + 1][u] = min(dp[i + 1][u], dp[i][v] + cost);
          }
        }
      }
      cout << min({dp[m][0], dp[m][1], dp[m][2], dp[m][3]}) << endl;
    } else if (n == 3) {
      for (int j = 0; j < (int)1000010; ++j)
        for (int k = 0; k < (int)(1 << 3); ++k) dp[j][k] = INT_MAX;
      int tmp = (v[0][0] - '0') * 4 + (v[1][0] - '0') * 2 + v[2][0] - '0';
      for (int bit = 0; bit < (1 << 3); bit++) {
        int cost = 0;
        for (int i = 0; i < 3; i++) {
          if (((bit >> i) & 1) != ((tmp >> i) & 1)) cost++;
        }
        dp[1][bit] = cost;
      }
      for (int i = 1; i < m; i++) {
        int tmp = (v[0][i] - '0') * 4 + (v[1][i] - '0') * 2 + v[2][i] - '0';
        for (int v = 0; v < (1 << 3); v++) {
          for (int u = 0; u < (1 << 3); u++) {
            int t = 0;
            for (int j = 0; j < 2; j++) {
              if ((v >> j) & 1) t++;
              if ((u >> j) & 1) t++;
            }
            if (t % 2 == 0) continue;
            t = 0;
            for (int j = 1; j < 3; j++) {
              if ((v >> j) & 1) t++;
              if ((u >> j) & 1) t++;
            }
            if (t % 2 == 0) continue;
            int cost = 0;
            for (int j = 0; j < 3; j++) {
              if (((u >> j) & 1) != ((tmp >> j) & 1)) cost++;
            }
            dp[i + 1][u] = min(dp[i + 1][u], dp[i][v] + cost);
          }
        }
      }
      long long ans = INT_MAX;
      for (int i = 0; i < (1 << 3); i++) {
        ans = min(ans, dp[m][i]);
      }
      cout << ans << endl;
    } else {
      if (m == 1) {
        cout << 0 << endl;
      } else if (m == 2) {
        for (int j = 0; j < (int)1000010; ++j)
          for (int k = 0; k < (int)(1 << 2); ++k) dp[j][k] = INT_MAX;
        int tmp = (v[0][0] - '0') * 2 + v[0][1] - '0';
        for (int bit = 0; bit < (1 << 2); bit++) {
          int cost = 0;
          for (int i = 0; i < 2; i++) {
            if (((bit >> i) & 1) != ((tmp >> i) & 1)) cost++;
          }
          dp[1][bit] = cost;
        }
        for (int i = 1; i < n; i++) {
          int tmp = (v[i][0] - '0') * 2 + v[i][1] - '0';
          for (int v = 0; v < (1 << 2); v++) {
            int t1 = 0;
            for (int j = 0; j < (1 << 2); j++) {
              if ((v >> j) & 1) t1++;
            }
            for (int u = 0; u < (1 << 2); u++) {
              int t2 = 0;
              for (int j = 0; j < (1 << 2); j++) {
                if ((u >> j) & 1) t2++;
              }
              if ((t1 + t2) % 2 == 0) continue;
              int cost = 0;
              for (int j = 0; j < 2; j++) {
                if (((u >> j) & 1) != ((tmp >> j) & 1)) cost++;
              }
              dp[i + 1][u] = min(dp[i + 1][u], dp[i][v] + cost);
            }
          }
        }
        cout << min({dp[n][0], dp[n][1], dp[n][2], dp[n][3]}) << endl;
      } else if (m == 3) {
        for (int j = 0; j < (int)1000010; ++j)
          for (int k = 0; k < (int)(1 << 3); ++k) dp[j][k] = INT_MAX;
        int tmp = (v[0][0] - '0') * 4 + (v[0][1] - '0') * 2 + v[0][3] - '0';
        for (int bit = 0; bit < (1 << 3); bit++) {
          int cost = 0;
          for (int i = 0; i < 3; i++) {
            if (((bit >> i) & 1) != ((tmp >> i) & 1)) cost++;
          }
          dp[1][bit] = cost;
        }
        for (int i = 1; i < n; i++) {
          int tmp = (v[i][0] - '0') * 4 + (v[i][1] - '0') * 2 + v[i][2] - '0';
          for (int v = 0; v < (1 << 3); v++) {
            for (int u = 0; u < (1 << 3); u++) {
              int t = 0;
              for (int j = 0; j < 2; j++) {
                if ((v >> j) & 1) t++;
                if ((u >> j) & 1) t++;
              }
              if (t % 2 == 0) continue;
              t = 0;
              for (int j = 1; j < 3; j++) {
                if ((v >> j) & 1) t++;
                if ((u >> j) & 1) t++;
              }
              if (t % 2 == 0) continue;
              int cost = 0;
              for (int j = 0; j < 3; j++) {
                if (((u >> j) & 1) != ((tmp >> j) & 1)) cost++;
              }
              dp[i + 1][u] = min(dp[i + 1][u], dp[i][v] + cost);
            }
          }
        }
        long long ans = INT_MAX;
        for (int i = 0; i < (1 << 3); i++) {
          ans = min(ans, dp[n][i]);
        }
        cout << ans << endl;
      }
    }
  }
  return 0;
}
