#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int one[8] = {0};
  for (int i = 1; i < 8; i++) one[i] = one[i / 2] + i % 2;
  {
    int n, m;
    cin >> n >> m;
    string mat[n];
    for (int i = 0; i < n; i++) {
      cin >> mat[i];
    }
    if (min(n, m) > 3) {
      cout << "-1";
      return 0;
    }
    int x = min(n, m);
    int y = n + m - x;
    int a[x][y];
    if (x == n) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) a[i][j] = mat[i][j] - '0';
      }
    } else {
      for (int i = 0; i < x; i++) {
        for (int j = 0; j < y; j++) a[i][j] = mat[j][i] - '0';
      }
    }
    if (x == 1) {
      cout << "0";
      return 0;
    } else if (x == 2) {
      int arr[2] = {0};
      for (int i = 0; i < y; i++) {
        int val = a[0][i] * 2 + a[1][i];
        if (i % 2 == 0) {
          arr[0] += min(one[val ^ 0], one[val ^ 3]);
          arr[1] += min(one[val ^ 1], one[val ^ 2]);
        } else {
          arr[1] += min(one[val ^ 0], one[val ^ 3]);
          arr[0] += min(one[val ^ 1], one[val ^ 2]);
        }
      }
      cout << min(arr[0], arr[1]);
    } else {
      int arr[4] = {0};
      for (int i = 0; i < y; i++) {
        int val = a[0][i] * 4 + a[1][i] * 2 + a[2][i];
        if (i % 2 == 0) {
          arr[0] += min(one[val ^ 0], one[val ^ 7]);
          arr[1] += min(one[val ^ 1], one[val ^ 6]);
          arr[2] += min(one[val ^ 2], one[val ^ 5]);
          arr[3] += min(one[val ^ 3], one[val ^ 4]);
        } else {
          arr[3] += min(one[val ^ 1], one[val ^ 6]);
          arr[2] += min(one[val ^ 0], one[val ^ 7]);
          arr[1] += min(one[val ^ 4], one[val ^ 3]);
          arr[0] += min(one[val ^ 5], one[val ^ 2]);
        }
      }
      cout << min(arr[2], min(arr[0], min(arr[3], arr[1])));
    }
  }
  return 0;
}
