#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N = 1000 + 10;
const int M = 100000 + 10;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
char s[4][1000005];
int dp[2][1000005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  if (n >= 4 && m >= 4) {
    for (int i = 1; i <= n; i++) {
      string ss;
      cin >> ss;
    }
    cout << -1 << endl;
  } else if (n < 2 || m < 2) {
    for (int i = 1; i <= n; i++) {
      string ss;
      cin >> ss;
    }
    cout << 0 << endl;
  } else {
    if (n == 2 || n == 3) {
      for (int i = 1; i <= n; i++) {
        cin >> s[i] + 1;
      }
      if (n == 2) {
        for (int i = 1; i <= m; i++) {
          if (s[1][i] == '1') dp[0][i]++;
          if (s[2][i] == '1') dp[0][i]++;
        }
        int ans1 = 0, ans2 = 0;
        for (int i = 1; i <= m; i++) {
          if (i & 1) {
            if (dp[0][i] & 1)
              ans1++;
            else
              ans2++;
          } else {
            if (dp[0][i] & 1)
              ans2++;
            else
              ans1++;
          }
        }
        cout << min(ans1, ans2) << endl;
      } else {
        for (int i = 1; i <= m; i++) {
          if (s[1][i] == '1') dp[0][i]++;
          if (s[2][i] == '1') {
            dp[0][i]++;
            dp[1][i]++;
          }
          if (s[3][i] == '1') dp[1][i]++;
        }
        int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
        for (int i = 1; i <= m; i++) {
          if (i & 1) {
            if (dp[0][i] & 1) {
              if (dp[1][i] & 1) {
                ans2++;
                ans3++;
                ans4++;
              } else {
                ans1++;
                ans2++;
                ans4++;
              }
            } else {
              if (dp[1][i] & 1) {
                ans1++;
                ans2++;
                ans3++;
              } else {
                ans1++;
                ans3++;
                ans4++;
              }
            }
          } else {
            if (dp[0][i] & 1) {
              if (dp[1][i] & 1) {
                ans1++;
                ans3++;
                ans4++;
              } else {
                ans1++;
                ans2++;
                ans3++;
              }
            } else {
              if (dp[1][i] & 1) {
                ans1++;
                ans2++;
                ans4++;
              } else {
                ans2++;
                ans3++;
                ans4++;
              }
            }
          }
        }
        cout << min(min(ans1, ans2), min(ans3, ans4)) << endl;
      }
    } else if (m == 2 || m == 3) {
      if (m == 2) {
        char ss[3];
        for (int i = 1; i <= n; i++) {
          cin >> ss;
          if (ss[0] == '1') dp[0][i]++;
          if (ss[1] == '1') dp[0][i]++;
        }
        int ans1 = 0, ans2 = 0;
        for (int i = 1; i <= n; i++) {
          if (i & 1) {
            if (dp[0][i] & 1)
              ans1++;
            else
              ans2++;
          } else {
            if (dp[0][i] & 1)
              ans2++;
            else
              ans1++;
          }
        }
        cout << min(ans1, ans2) << endl;
      } else {
        char ss[5];
        for (int i = 1; i <= n; i++) {
          cin >> ss;
          s[1][i] = ss[0];
          s[2][i] = ss[1];
          s[3][i] = ss[2];
        }
        for (int i = 1; i <= n; i++) {
          if (s[1][i] == '1') dp[0][i]++;
          if (s[2][i] == '1') {
            dp[0][i]++;
            dp[1][i]++;
          }
          if (s[3][i] == '1') dp[1][i]++;
        }
        int ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
        for (int i = 1; i <= n; i++) {
          if (i & 1) {
            if (dp[0][i] & 1) {
              if (dp[1][i] & 1) {
                ans2++;
                ans3++;
                ans4++;
              } else {
                ans1++;
                ans2++;
                ans4++;
              }
            } else {
              if (dp[1][i] & 1) {
                ans1++;
                ans2++;
                ans3++;
              } else {
                ans1++;
                ans3++;
                ans4++;
              }
            }
          } else {
            if (dp[0][i] & 1) {
              if (dp[1][i] & 1) {
                ans1++;
                ans3++;
                ans4++;
              } else {
                ans1++;
                ans2++;
                ans3++;
              }
            } else {
              if (dp[1][i] & 1) {
                ans1++;
                ans2++;
                ans4++;
              } else {
                ans2++;
                ans3++;
                ans4++;
              }
            }
          }
        }
        cout << min(min(ans1, ans2), min(ans3, ans4)) << endl;
      }
    }
  }
  return 0;
}
