#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  if (n == 1) {
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
    }
    cout << "0" << endl;
    return 0;
  }
  if (n == 2) {
    string s1;
    string s2;
    cin >> s1;
    cin >> s2;
    int count1 = 0;
    int count0 = 0;
    for (int i = 0; i < m; i++) {
      if ((s1[i] - '0' + s2[i] - '0') % 2 == 1) {
        count1 += i % 2;
        count0 += (i + 1) % 2;
      } else {
        count0 += i % 2;
        count1 += (i + 1) % 2;
      }
    }
    if (count0 < count1)
      cout << count0 << endl;
    else
      cout << count1 << endl;
    return 0;
  }
  if (n == 3) {
    string s1;
    string s2;
    string s3;
    cin >> s1;
    cin >> s2;
    cin >> s3;
    int count00 = 0;
    int count01 = 0;
    int count10 = 0;
    int count11 = 0;
    for (int i = 0; i < m; i++) {
      if ((s1[i] - '0' + s2[i] - '0') % 2 == 1) {
        if ((s2[i] - '0' + s3[i] - '0') % 2 == 1) {
          if (i % 2 == 0) {
            count00 += 1;
            count01 += 1;
            count11 += 0;
            count10 += 1;
          } else {
            count00 += 0;
            count01 += 1;
            count11 += 1;
            count10 += 1;
          }
        } else {
          if (i % 2 == 0) {
            count00 += 1;
            count01 += 1;
            count11 += 1;
            count10 += 0;
          } else {
            count00 += 1;
            count01 += 0;
            count11 += 1;
            count10 += 1;
          }
        }
      } else {
        if ((s2[i] - '0' + s3[i] - '0') % 2 == 1) {
          if (i % 2 == 0) {
            count00 += 1;
            count01 += 0;
            count11 += 1;
            count10 += 1;
          } else {
            count00 += 1;
            count01 += 1;
            count11 += 1;
            count10 += 0;
          }
        } else {
          if (i % 2 == 0) {
            count00 += 0;
            count01 += 1;
            count11 += 1;
            count10 += 1;
          } else {
            count00 += 1;
            count01 += 1;
            count11 += 0;
            count10 += 1;
          }
        }
      }
    }
    int min = count00;
    if (count01 < min) min = count01;
    if (count11 < min) min = count11;
    if (count10 < min) min = count10;
    cout << min << endl;
    return 0;
  }
  if (m >= 4) {
    for (int i = 0; i < n; i++) {
      string s;
      cin >> s;
    }
    cout << "-1" << endl;
    return 0;
  }
}
