#include <bits/stdc++.h>
using namespace std;
void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
const long long mod = 1000000007;
int tablica[2];
long long potega(long long base, long long pot) {
  if (pot == 0) return 1;
  if (pot == 1) return base;
  if (pot % 2 == 0) {
    long long res = potega(base, pot / 2);
    return ((res * res) % mod);
  } else
    return ((potega(base, pot - 1) * base) % mod);
}
int bs(int pocz, int kon, int szu) {
  int sr;
  while (pocz < kon) {
    sr = (pocz + kon) / 2;
    if (tablica[sr] >= szu)
      kon = sr;
    else
      pocz = sr + 1;
  }
  return pocz;
}
int n, m, tab[1000007][5], dp[1000007][10];
int czy[4][10][10];
string s;
void reset_arrays() {}
void solve() {
  reset_arrays();
  cin >> n >> m;
  if (min(n, m) > 3) {
    cout << "-1\n";
    return;
  }
  if (min(n, m) == 1) {
    cout << "0\n";
    return;
  }
  if (n > m) {
    swap(n, m);
    for (int x = 1; x <= m; x++) {
      cin >> s;
      for (int y = 1; y <= n; y++) tab[x][y] = s[y - 1] - '0';
    }
  } else {
    for (int y = 1; y <= n; y++) {
      cin >> s;
      for (int x = 1; x <= m; x++) tab[x][y] = s[x - 1] - '0';
    }
  }
  czy[2][0][1] = czy[2][0][2] = czy[2][1][0] = czy[2][1][3] = czy[2][2][0] =
      czy[2][2][3] = czy[2][3][1] = czy[2][3][2] = true;
  czy[3][0][2] = czy[3][0][5] = czy[3][1][3] = czy[3][1][4] = czy[3][2][0] =
      czy[3][2][7] = czy[3][3][1] = czy[3][3][6] = true;
  czy[3][4][1] = czy[3][4][6] = czy[3][5][0] = czy[3][5][7] = czy[3][6][3] =
      czy[3][6][4] = czy[3][7][2] = czy[3][7][5] = true;
  int maska = tab[1][1] + 2 * tab[1][2] + 4 * tab[1][3];
  for (int i = 0; i < 8; i++) {
    int ile = 0;
    int i1 = i & 1;
    int i2 = i & 2;
    int i4 = i & 4;
    int maska1 = maska & 1;
    int maska2 = maska & 2;
    int maska4 = maska & 4;
    if (i1 != maska1) ile++;
    if (i2 != maska2) ile++;
    if (i4 != maska4) ile++;
    dp[1][i] = ile;
  }
  for (int i = 2; i <= m; i++) {
    int poczatkowo = tab[i][1] + 2 * tab[i][2] + 4 * tab[i][3];
    for (int maska = 0; maska < 8; maska++) {
      dp[i][maska] = 1000000007;
      for (int pop_maska = 0; pop_maska < 8; pop_maska++) {
        if (czy[n][pop_maska][maska] == true) {
          int ile = 0;
          int maska1 = maska & 1;
          int maska2 = maska & 2;
          int maska4 = maska & 4;
          int poczatkowo1 = poczatkowo & 1;
          int poczatkowo2 = poczatkowo & 2;
          int poczatkowo4 = poczatkowo & 4;
          if (poczatkowo1 != maska1) ile++;
          if (poczatkowo2 != maska2) ile++;
          if (poczatkowo4 != maska4) ile++;
          dp[i][maska] = min(dp[i][maska], dp[i - 1][pop_maska] + ile);
        }
      }
    }
  }
  int odp = 1000000007;
  for (int i = 0; i < 8; i++) odp = min(odp, dp[m][i]);
  cout << odp;
}
int main() {
  boost();
  int t = 1;
  while (t--) solve();
  return 0;
}
