#include <bits/stdc++.h>   using namespace std;   int n, m;   char jz[1000006][5];   int cnt2 = 0, cnt3 = 0, dp[1000006][8];   int cal(int x) {     int ans = 0;     while (x > 0) {       if (x & 1) ans++;       x /= 2;     }     return ans;   }   bool check_3(int x, int y) {     int sum = cal(x / 2) + cal(y / 2);     if (sum % 2 == 0) return false;     if (x >= 4) x -= 4;     if (y >= 4) y -= 4;     sum = cal(x) + cal(y);     if (sum % 2 == 0) return false;     return true;   }   bool check_2(int x, int y) {     if ((cal(x) + cal(y)) % 2 == 0) return false;     return true;   }   int change(int x, int y) { return cal(x ^ y); }   struct s3 {     int x;     vector<int> v;   } a3[10], a2[5];   int main() {     memset(dp, 0x3f3f3f3f, sizeof(dp));     cin >> n >> m;     if (n > 3 && m > 3) {       cout << -1;       return 0;     }     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         if (m >= 4)           cin >> jz[j][i];         else if (m < 4)           cin >> jz[i][j];       }       getchar();     }     if (m >= 4) swap(n, m);     if (m == 1) {       cout << 0;       return 0;     } else if (m == 2) {       for (int i = 0; i < 4; i++)         for (int j = 0; j < 4; j++) {           if (check_2(i, j)) {             a2[i].v.push_back(j);           }         }       if (n == 1) {         cout << 0;         return 0;       }       int num1 = 0;       for (int i = 1; i <= m; i++) {         num1 *= 2;         if (jz[1][i] ==  1 ) num1++;       }       for (int i = 2; i <= n; i++) {         int num = 0;         for (int l = 1; l <= m; l++) {           num *= 2;           if (jz[i][l] ==  1 ) num++;         }         if (i == 2) {           for (int j = 0; j < 4; j++) {             for (int k = 0; k < a2[j].v.size(); k++)               dp[i][j] = min(dp[i][j], change(j, num) + change(a2[j].v[k], num1));           }         } else {           for (int j = 0; j < 4; j++) {             for (int k = 0; k < a2[j].v.size(); k++)               dp[i][j] = min(dp[i][j], dp[i - 1][a2[j].v[k]] + change(j, num));           }         }       }       int ans = 2100000000;       for (int i = 0; i < 4; i++) ans = min(ans, dp[n][i]);       cout << ans;     } else if (m == 3) {       for (int i = 0; i <= 7; i++) {         for (int j = 0; j <= 7; j++) {           if (check_3(i, j)) {             a3[i].x = i;             a3[i].v.push_back(j);           }         }       }       int num1 = 0;       for (int i = 1; i <= m; i++) {         num1 *= 2;         if (jz[1][i] ==  1 ) num1++;       }       if (n == 1) {         cout << 0;         return 0;       }       for (int i = 2; i <= n; i++) {         int num = 0;         for (int l = 1; l <= m; l++) {           num *= 2;           if (jz[i][l] ==  1 ) num++;         }         if (i == 2) {           for (int j = 0; j < 8; j++)             for (int k = 0; k < a3[j].v.size(); k++) {               dp[i][j] = min(dp[i][j], change(j, num) + change(a3[j].v[k], num1));             }         } else {           for (int j = 0; j < 8; j++)             for (int k = 0; k < a3[j].v.size(); k++) {               dp[i][j] = min(dp[i][j], dp[i - 1][a3[j].v[k]] + change(j, num));             }         }       }       int ans = 2100000000;       for (int i = 0; i < 8; i++) ans = min(ans, dp[n][i]);       cout << ans;     }   }