#include <bits/stdc++.h>
using namespace std;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const long long INF = 2 * 1e18;
const long double PI = 3.14159265358979323846;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<vector<char> > s(n, vector<char>(m));
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) cin >> s[i][j];
  if (n >= 4 && m >= 4)
    cout << -1;
  else {
    if (n == 1 || m == 1) {
      cout << 0;
      return 0;
    }
    if (n == 2) {
      long long cnt1 = 0, cnt2 = 0;
      for (long long i = 0; i < m; i++) {
        if (i % 2 == 0) {
          if (s[0][i] == s[1][i])
            cnt1++;
          else
            cnt2++;
        } else {
          if (s[0][i] != s[1][i])
            cnt1++;
          else
            cnt2++;
        }
      }
      cout << min(cnt1, cnt2);
      return 0;
    }
    if (m == 2) {
      long long cnt1 = 0, cnt2 = 0;
      for (long long i = 0; i < n; i++) {
        if (i % 2 == 0) {
          if (s[i][0] == s[i][1])
            cnt1++;
          else
            cnt2++;
        } else {
          if (s[i][0] != s[i][1])
            cnt1++;
          else
            cnt2++;
        }
      }
      cout << min(cnt1, cnt2);
      return 0;
    }
    if (n == 3) {
      long long cnt1 = m, cnt2 = m, cnt3 = m, cnt4 = m;
      for (long long i = 0; i < m; i++) {
        if (i % 2 == 0) {
          if (s[0][i] == s[1][i] && s[1][i] == s[2][i])
            cnt1--;
          else if (s[0][i] == s[1][i] && s[1][i] != s[2][i])
            cnt2--;
          else if (s[0][i] != s[1][i] && s[1][i] == s[2][i])
            cnt3--;
          else
            cnt4--;
        } else {
          if (s[0][i] != s[1][i] && s[1][i] != s[2][i])
            cnt1--;
          else if (s[0][i] != s[1][i] && s[1][i] == s[2][i])
            cnt2--;
          else if (s[0][i] == s[1][i] && s[1][i] != s[2][i])
            cnt3--;
          else
            cnt4--;
        }
      }
      cout << min(cnt1, min(cnt2, min(cnt3, cnt4)));
      return 0;
    }
    if (m == 3) {
      long long cnt1 = n, cnt2 = n, cnt3 = n, cnt4 = n;
      for (long long i = 0; i < n; i++) {
        if (i % 2 == 0) {
          if (s[i][0] == s[i][1] && s[i][1] == s[i][2])
            cnt1--;
          else if (s[i][0] == s[i][1] && s[i][1] != s[i][2])
            cnt2--;
          else if (s[i][0] != s[i][1] && s[i][1] == s[i][2])
            cnt3--;
          else
            cnt4--;
        } else {
          if (s[i][0] != s[i][1] && s[i][1] != s[i][2])
            cnt1--;
          else if (s[i][0] != s[i][1] && s[i][1] == s[i][2])
            cnt2--;
          else if (s[i][0] == s[i][1] && s[i][1] != s[i][2])
            cnt3--;
          else
            cnt4--;
        }
      }
      cout << min(cnt1, min(cnt2, min(cnt3, cnt4)));
      return 0;
    }
  }
}
