#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T>
inline ostream& operator<<(ostream& os, vector<T> arr) {
  os << "[";
  for (int i = 0; i < (int)arr.size(); i++)
    os << arr[i] << (i == (int)arr.size() - 1 ? "]" : ",");
  os << endl;
  return os;
}
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}
const int INF = 1e9;
int dima;
int f(int a, int b) {
  int cnt = 0;
  for (int i = 0; i < 4; i++)
    if ((a & (1 << i)) != (b & (1 << i))) cnt++;
  return cnt;
}
int bin(vector<int> a) {
  int cnt = 0;
  int m = a.size();
  for (int i = 0; i < m; i++)
    if (a[i]) cnt += (1 << i);
  return cnt;
}
bool chack(int a, int b) {
  if ((((a & (1 << 0)) != 0) + ((a & (1 << 1)) != 0) + ((b & (1 << 0)) != 0) +
       ((b & (1 << 1)) != 0)) %
          2 ==
      0)
    return false;
  if (dima > 2) {
    if ((((a & (1 << 2)) != 0) + ((a & (1 << 1)) != 0) + ((b & (1 << 2)) != 0) +
         ((b & (1 << 1)) != 0)) %
            2 ==
        0)
      return false;
  }
  return true;
}
int main() {
  int h, w;
  cin >> h >> w;
  vector<vector<int>> num(h, vector<int>(w));
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      num[i][j] = c - '0';
    }
  vector<vector<int>> arr(max(h, w), vector<int>(min(h, w)));
  if (h >= 4 && w >= 4) {
    cout << -1 << endl;
    return 0;
  }
  if (min(h, w) == 1) {
    cout << 0 << endl;
    return 0;
  }
  if (w > h) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        arr[j][h - 1 - i] = num[i][j];
      }
    }
  } else
    arr = num;
  int n = max(h, w), m = min(h, w);
  dima = m;
  vector<vector<int>> dp(n + 1, vector<int>(1 << m, 1e9));
  int tmp = bin(arr[0]);
  for (int i = 0; i < (1 << m); i++) dp[0][i] = f(tmp, i);
  for (int i = 0; i < n - 1; i++) {
    for (int j = 0; j < (1 << m); j++) {
      tmp = bin(arr[i + 1]);
      for (int k = 0; k < (1 << m); k++) {
        if (!chack(j, k)) continue;
        dp[i + 1][k] = min(dp[i + 1][k], dp[i][j] + f(tmp, k));
      }
    }
  }
  cout << (*min_element(dp[n - 1].begin(), dp[n - 1].end()) == INF
               ? -1
               : *min_element(dp[n - 1].begin(), dp[n - 1].end()))
       << endl;
}
