#include <bits/stdc++.h>
using namespace std;
vector<string> arr;
int dp2[1000001][4];
int dp3[1000001][8];
int bitmask[1000001];
int match2[4][2] = {1, 2, 0, 3, 0, 3, 1, 2};
int match3[8][2] = {2, 5, 3, 4, 7, 0, 1, 6, 1, 6, 7, 0, 3, 4, 2, 5};
int bit_cost(int a, int b) {
  int cost = a ^ b;
  int ret = 0;
  while (cost) {
    if (cost % 2) ret++;
    cost /= 2;
  }
  return ret;
}
int f2(int index, int mask) {
  int& ret = dp2[index][mask];
  if (~ret) return ret;
  int cost = bit_cost(bitmask[index], mask);
  if (index == 0) return ret = cost;
  ret = 100000000;
  for (int i = 0; i < 2; i++) {
    ret = min(ret, f2(index - 1, match2[mask][i]));
  }
  return ret += cost;
}
int f3(int index, int mask) {
  int& ret = dp3[index][mask];
  if (~ret) return ret;
  int cost = bit_cost(bitmask[index], mask);
  if (index == 0) return ret = cost;
  ret = 100000000;
  for (int i = 0; i < 2; i++) {
    ret = min(ret, f3(index - 1, match3[mask][i]));
  }
  return ret += cost;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    string s;
    cin >> s;
    arr.push_back(s);
  }
  if (n >= 4)
    cout << -1;
  else if (n == 1)
    cout << 0;
  else if (n == 2) {
    memset(dp2, -1, sizeof(dp2));
    for (int i = 0; i < m; i++) {
      int bit = 0;
      for (int j = 0; j < 2; j++) {
        if (arr[j][i] == '1') bit += (1 << j);
      }
      bitmask[i] = bit;
    }
    int maxchange = 100000000;
    for (int i = 0; i < 4; i++) {
      maxchange = min(maxchange, f2(m - 1, i));
    }
    cout << maxchange;
  } else {
    memset(dp3, -1, sizeof(dp3));
    for (int i = 0; i < m; i++) {
      int bit = 0;
      for (int j = 0; j < 3; j++) {
        if (arr[j][i] == '1') bit += (1 << j);
      }
      bitmask[i] = bit;
    }
    int maxchange = 100000000;
    for (int i = 0; i < 8; i++) {
      maxchange = min(maxchange, f3(m - 1, i));
    }
    cout << maxchange;
  }
}
