#include <bits/stdc++.h>
using namespace std;
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c *x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct deb {
  template <class c>
  deb &operator<<(const c &) {
    return *this;
  }
};
template <class T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <class T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
const long long dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const long long dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const long long mod = 1e9 + 7;
inline long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
inline long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
inline long long mul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
  for (auto &v : vec) is >> v;
  return is;
}
const double PI = acos(-1.0);
const long long inf = 1e18;
const long long maxn = 1e3 + 5;
long long diff(long long a, long long b) {
  long long cost = 0;
  while (a || b) {
    cost += ((a & 1) != (b & 1));
    a = a >> 1;
    b = b >> 1;
  }
  return cost;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<string> arr(n);
  cin >> arr;
  if (n >= 4 && m >= 4) {
    cout << -1 << "\n";
    return;
  }
  if (min(n, m) == 1) {
    cout << 0 << "\n";
    return;
  }
  vector<vector<long long>> mat;
  if (n > m) {
    mat = vector<vector<long long>>(m, vector<long long>(n));
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j < m; j++) {
        mat[j][i] = arr[i][j] - '0';
      }
    swap(n, m);
  } else {
    mat = vector<vector<long long>>(n, vector<long long>(m));
    for (long long i = 0; i < n; i++)
      for (long long j = 0; j < m; j++) {
        mat[i][j] = arr[i][j] - '0';
      }
  }
  vector<long long> a(m);
  for (long long i = 0; i < m; i++) {
    long long val = 0;
    for (long long j = 0; j < n; j++) {
      val += mat[j][i] * (1 << j);
    }
    a[i] = val;
  }
  deb() << " ["
        << "a"
           ": "
        << (a) << "] ";
  long long M;
  vector<vector<long long>> trans;
  if (n == 2) {
    M = 4;
    trans = vector<vector<long long>>(M);
    trans[0] = {1, 2};
    trans[1] = {0, 3};
    trans[2] = {0, 3};
    trans[3] = {1, 2};
  } else {
    M = 8;
    trans = vector<vector<long long>>(M);
    trans[0] = {2, 5};
    trans[1] = {4, 3};
    trans[2] = {0, 7};
    trans[3] = {1, 6};
    trans[4] = {1, 6};
    trans[5] = {0, 7};
    trans[6] = {4, 3};
    trans[7] = {2, 5};
  }
  vector<vector<long long>> dp(m + 1, vector<long long>(M));
  long long val = inf;
  for (long long j = m - 1; j >= 0; j--) {
    for (long long mask = 0; mask < M; mask++) {
      long long &ans = dp[j][mask];
      ans = inf;
      long long cost = diff(a[j], mask);
      for (auto t : trans[mask]) {
        ans = min(ans, cost + dp[j + 1][t]);
      }
      if (j == 0) val = min(val, dp[j][mask]);
    }
  }
  cout << val << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  for (long long tt = 1; tt <= t; tt++) {
    solve();
  }
}
