#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
const long double pi = 3.14159265359;
const long long N = 1e5 + 5;
long long dp[8][1000005];
long long power(long long a, long long b) {
  long long res = 1;
  a = a % mod;
  while (b > 0) {
    if (b % 2 == 1) {
      res = (res * a) % mod;
    }
    b /= 2;
    a = (a * a) % mod;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  string a[n];
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (n >= 4 && m >= 4) {
    cout << "-1";
  } else if (n == 1 || m == 1) {
    cout << "0";
  } else if (n == 2) {
    long long dp[m][2];
    if ((a[0][0] == '0' && a[1][0] == '1') ||
        (a[0][0] == '1' && a[1][0] == '0')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 0;
    }
    for (int i = 1; i < m; i++) {
      if ((a[0][i] == '0' && a[1][i] == '1') ||
          (a[0][i] == '1' && a[1][i] == '0')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      }
    }
    cout << min(dp[m - 1][0], dp[m - 1][1]) << "\n";
  } else if (m == 2) {
    long long dp[n][2];
    if ((a[0][0] == '0' && a[0][1] == '1') ||
        (a[0][0] == '1' && a[0][1] == '0')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 0;
    }
    for (int i = 1; i < n; i++) {
      if ((a[i][0] == '0' && a[i][1] == '1') ||
          (a[i][0] == '1' && a[i][1] == '0')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      }
    }
    cout << min(dp[n - 1][0], dp[n - 1][1]) << "\n";
  } else if (m == 3) {
    long long dp[n][2];
    if ((a[0][0] == '0' && a[0][1] == '0' && a[0][2] == '0') ||
        (a[0][0] == '1' && a[0][1] == '1' && a[0][2] == '1')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else if ((a[0][0] == '1' && a[0][1] == '0' && a[0][2] == '1') ||
               (a[0][0] == '0' && a[0][1] == '1' && a[0][2] == '0')) {
      dp[0][0] = 1;
      dp[0][1] = 0;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 1;
    }
    for (int i = 1; i < n; i++) {
      if ((a[i][0] == '0' && a[i][0] == '0' && a[i][2] == '0') ||
          (a[i][0] == '1' && a[i][1] == '1' && a[i][2] == '1')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else if ((a[i][0] == '1' && a[i][1] == '0' && a[i][2] == '1') ||
                 (a[i][0] == '0' && a[i][1] == '1' && a[i][2] == '0')) {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      }
    }
    long long x = min(dp[n - 1][0], dp[n - 1][1]);
    if ((a[0][0] == '0' && a[0][1] == '0' && a[0][2] == '1') ||
        (a[0][0] == '1' && a[0][1] == '1' && a[0][2] == '0')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else if ((a[0][0] == '0' && a[0][1] == '1' && a[0][2] == '1') ||
               (a[0][0] == '1' && a[0][1] == '0' && a[0][2] == '0')) {
      dp[0][0] = 1;
      dp[0][1] = 0;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 1;
    }
    for (int i = 1; i < n; i++) {
      if ((a[i][0] == '0' && a[i][0] == '0' && a[i][2] == '1') ||
          (a[i][0] == '1' && a[i][1] == '1' && a[i][2] == '0')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else if ((a[i][0] == '0' && a[i][1] == '1' && a[i][2] == '1') ||
                 (a[i][0] == '1' && a[i][1] == '0' && a[i][2] == '0')) {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      }
    }
    x = min(x, min(dp[n - 1][0], dp[n - 1][1]));
    cout << x << "\n";
  } else if (n == 3) {
    long long dp[m][2];
    if ((a[0][0] == '0' && a[1][0] == '0' && a[2][0] == '0') ||
        (a[0][0] == '1' && a[1][0] == '1' && a[2][0] == '1')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else if ((a[0][0] == '1' && a[1][0] == '0' && a[2][0] == '1') ||
               (a[0][0] == '0' && a[1][0] == '1' && a[2][0] == '0')) {
      dp[0][0] = 1;
      dp[0][1] = 0;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 1;
    }
    for (int i = 1; i < m; i++) {
      if ((a[0][i] == '0' && a[1][i] == '0' && a[2][i] == '0') ||
          (a[0][i] == '1' && a[1][i] == '1' && a[2][i] == '1')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else if ((a[0][i] == '1' && a[1][i] == '0' && a[2][i] == '1') ||
                 (a[0][i] == '0' && a[1][i] == '1' && a[2][i] == '0')) {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      }
    }
    long long x = min(dp[m - 1][0], dp[m - 1][1]);
    if ((a[0][0] == '0' && a[1][0] == '0' && a[2][0] == '1') ||
        (a[0][0] == '1' && a[1][0] == '1' && a[2][0] == '0')) {
      dp[0][0] = 0;
      dp[0][1] = 1;
    } else if ((a[0][0] == '0' && a[1][0] == '1' && a[2][0] == '1') ||
               (a[0][0] == '1' && a[1][0] == '0' && a[2][0] == '0')) {
      dp[0][0] = 1;
      dp[0][1] = 0;
    } else {
      dp[0][0] = 1;
      dp[0][1] = 1;
    }
    for (int i = 1; i < m; i++) {
      if ((a[0][i] == '0' && a[1][i] == '0' && a[2][i] == '1') ||
          (a[0][i] == '1' && a[1][i] == '1' && a[2][i] == '0')) {
        dp[i][0] = dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      } else if ((a[0][i] == '0' && a[1][i] == '1' && a[2][i] == '1') ||
                 (a[0][i] == '1' && a[1][i] == '0' && a[2][i] == '0')) {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = dp[i - 1][0];
      } else {
        dp[i][0] = 1 + dp[i - 1][1];
        dp[i][1] = 1 + dp[i - 1][0];
      }
    }
    x = min(x, min(dp[m - 1][0], dp[m - 1][1]));
    cout << x << "\n";
  }
}
