#include <bits/stdc++.h>
int a[1000001];
int b[4][1000001];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  if (n >= 4) {
    printf("%d", -1);
    return 0;
  }
  if (n == 1) {
    printf("%d", 0);
    return 0;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      scanf("%1d", &b[i][j]);
    }
  }
  if (n == 2) {
    for (int i = 0; i < m; ++i) {
      int x, y;
      x = b[0][i], y = b[1][i];
      a[i] = x ^ y;
    }
    int r = n * m;
    for (int k = 0; k < 2; ++k) {
      int c = 0;
      for (int i = 0; i < m; ++i) {
        if (a[i] != (k + i) % 2) ++c;
      }
      if (r > c) r = c;
    }
    printf("%d", r);
  }
  if (n == 3) {
    for (int i = 0; i < m; ++i) {
      int x, y, z;
      x = b[0][i], y = b[1][i], z = b[2][i];
      a[i] = ((x ^ y) << 1) | (y ^ z);
    }
    int r = n * m;
    for (int k = 0; k < 4; ++k) {
      int c = 0;
      for (int i = 0; i < m; ++i) {
        if (i % 2 == 1) {
          if (a[i] != 3 - k) ++c;
        } else {
          if (a[i] != k) ++c;
        }
      }
      if (r > c) r = c;
    }
    printf("%d", r);
  }
}
