#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int maxn = 1e6 + 7;
const int INF = 1e9;
const long long INFF = 1e18;
int n, m, ans = INF, arr[4][maxn], dp[maxn][9];
int g[8][3] = {{5, 2}, {4, 3}, {7, 0}, {1, 6}, {1, 6}, {0, 7}, {4, 3}, {5, 2}};
int check(int x, int pos) {
  int x1 = (x / 4) ^ arr[1][pos];
  int x2 = ((x % 4) / 2) ^ arr[2][pos];
  int x3 = (x % 2) ^ arr[3][pos];
  return x1 + x2 + x3;
}
int main() {
  scanf("%d%d", &n, &m);
  if (n >= 4)
    printf("%d\n", -1);
  else if (n == 1)
    printf("%d\n", 0);
  else {
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) scanf("%1d", &arr[i][j]);
    if (n == 2) {
      int cnt1 = 0, cnt2 = 0;
      for (int j = 1; j <= m; j++) {
        int x = arr[1][j] + arr[2][j];
        if (j % 2 == 1)
          x != 1 ? cnt1++ : cnt2++;
        else
          x == 1 ? cnt1++ : cnt2++;
      }
      printf("%d\n", min(cnt1, cnt2));
    } else {
      for (int j = 0; j < 8; j++) dp[1][j] = check(j, 1);
      for (int i = 2; i <= m; i++)
        for (int j = 0; j < 8; j++)
          dp[i][j] = check(j, i) + min(dp[i - 1][g[j][0]], dp[i - 1][g[j][1]]);
      for (int j = 0; j < 8; j++) ans = min(ans, dp[m][j]);
      printf("%d\n", ans);
    }
  }
  return 0;
}
