#include <bits/stdc++.h>
using namespace std;
const long long linf = 4000000000000000000LL;
const int inf = 1000000007;
const long double pi = 3.1415926535;
long long maximum(long long x, long long y) {
  if (x > y) return x;
  return y;
}
long long minimum(long long x, long long y) {
  if (x < y) return x;
  return y;
}
void pv(vector<int> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pv(vector<long long> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pv(vector<double> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pv(vector<string> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pv(vector<bool> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pv(vector<pair<int, int>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).first << " ";
  cout << '\n';
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).second << " ";
  cout << '\n';
}
void pv(vector<pair<long long, long long>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).first << " ";
  cout << '\n';
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).second << " ";
  cout << '\n';
}
void pv(vector<pair<pair<int, int>, int>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).first.first << " " << (*i).first.second << " " << (*i).second
         << '\n';
  cout << '\n';
}
void pv(vector<pair<int, pair<int, int>>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << (*i).first << " " << (*i).second.first << " " << (*i).second.second
         << '\n';
  cout << '\n';
}
void pv(vector<vector<string>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<int>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<long long>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<pair<long long, long long>>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
  }
  cout << '\n';
}
void pv(vector<vector<vector<int>>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<vector<long long>>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<bool>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    pv(*i);
    cout << '\n';
  }
  cout << '\n';
}
void ps(set<int> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void ps(set<pair<int, int>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++)
    cout << i->first << " " << i->second << '\n';
  cout << '\n';
}
void pv(vector<set<int>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    ps(*i);
    cout << '\n';
  }
  cout << '\n';
}
void pv(vector<vector<pair<int, int>>> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) {
    cout << i - amount.begin() << '\n';
    pv(*i);
  }
}
void ps(set<string> amount) {
  for (auto i = amount.begin(); i != amount.end(); i++) cout << *i << " ";
  cout << '\n';
}
void pm(map<int, vector<int>> m) {
  for (map<int, vector<int>>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i->first << " ";
    pv(i->second);
  }
}
void pm(map<string, vector<string>> m) {
  for (map<string, vector<string>>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i->first << " ";
    pv(i->second);
  }
}
void pm(map<string, int> m) {
  for (auto i = m.begin(); i != m.end(); i++) {
    cout << i->first << " " << i->second << '\n';
  }
}
void pm(map<string, vector<bool>> m) {
  for (map<string, vector<bool>>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i->first << " ";
    pv(i->second);
  }
}
void pm(map<string, set<string>> m) {
  for (auto i = m.begin(); i != m.end(); i++) {
    cout << i->first << " ";
    ps(i->second);
  }
}
void pm(map<int, int> m) {
  for (map<int, int>::iterator i = m.begin(); i != m.end(); i++) {
    cout << i->first << " " << i->second << '\n';
  }
}
void pm(map<char, vector<string>> m) {
  for (auto i = m.begin(); i != m.end(); i++) {
    cout << i->first << '\n';
    cout << '\t';
    pv(i->second);
  }
}
void pm(map<char, int> m) {
  for (auto i = m.begin(); i != m.end(); i++) {
    cout << i->first << '\n';
    cout << '\t' << i->second << '\n';
  }
}
void pm(map<pair<int, int>, int> m) {
  for (auto i = m.begin(); i != m.end(); i++) {
    cout << i->first.first << " " << i->first.second << " " << i->second
         << '\n';
  }
}
bool comp(pair<int, int> x, pair<int, int> y) {
  if (x.second == y.second) return x.first < y.first;
  return x.second < y.second;
}
int modInverse(int a, int m) {
  int m0 = m;
  int y = 0, x = 1;
  if (m == 1) return 0;
  while (a > 1) {
    int q = a / m;
    int t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0) x += m0;
  return x;
}
long long power(long long x, long long y) {
  long long k = 1 << 30;
  long long z = 1;
  while (k != 0) {
    z *= z;
    z %= inf;
    if (y >= k) {
      z *= x;
      z %= inf;
      y -= k;
    }
    k >>= 1;
  }
  return z;
}
long double area(pair<int, int> x, pair<int, int> y, pair<int, int> z) {
  return ((long long)x.second * y.first + (long long)y.second * z.first +
          (long long)z.second * x.first - (long long)x.first * y.second -
          (long long)y.first * z.second - (long long)z.first * x.second) /
         2.0;
}
bool clockwise(pair<int, int> x, pair<int, int> y, pair<int, int> z) {
  return area(x, y, z) > 0;
}
struct point {
  long double x, y;
};
long double area(point x, point y, point z) {
  return (x.y * y.x + y.y * z.x + z.y * x.x - x.x * y.y - y.x * z.y -
          z.x * x.y) /
         2.0;
}
bool clockwise(point x, point y, point z) { return area(x, y, z) > 0; }
int gcd(int a, int b) {
  if (a > b) swap(a, b);
  if (a == 0) return b;
  return gcd(a, b % a);
}
int popcount(int a) {
  int count = 0;
  while (a != 0) {
    count += (a % 2);
    a >>= 1;
  }
  return count;
}
long long a, b, c;
void setIO(string second) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
int n, m;
vector<vector<char>> A;
void init() {
  setIO("shopping");
  cin >> n >> m;
  A.resize(n);
  for (int i = (0); i < (n); ++i) A[i].resize(m);
  for (int i = (0); i < (n); ++i) {
    string t;
    cin >> t;
    for (int j = (0); j < (m); ++j) A[i][j] = t[j];
  }
}
int total;
vector<vector<long long>> dp;
int dist_(int i, int mask) {
  int a = 0;
  for (int j = (0); j < (n); ++j)
    if (((mask & (1 << j)) && A[j][i] == '0') ||
        ((mask & (1 << j)) == 0 && A[j][i] == '1'))
      a++;
  return a;
}
bool check(int mask1, int mask2) {
  if (n == 2) {
    int a = 0;
    for (int i = (0); i < (2); ++i)
      if (mask1 & (1 << i)) a++;
    int b = 0;
    for (int i = (0); i < (2); ++i)
      if (mask2 & (1 << i)) b++;
    if ((a + b) % 2 == 1)
      return 1;
    else
      return 0;
  } else {
    int a = 0;
    for (int i = (0); i < (2); ++i)
      if (mask1 & (1 << i)) a++;
    int b = 0;
    for (int i = (0); i < (2); ++i)
      if (mask2 & (1 << i)) b++;
    if ((a + b) % 2 == 0) return 0;
    a = 0;
    b = 0;
    for (int i = (0); i < (2); ++i)
      if (mask1 & (1 << i + 1)) a++;
    for (int i = (0); i < (2); ++i)
      if (mask2 & (1 << i + 1)) b++;
    if ((a + b) % 2 == 0) return 0;
    return 1;
  }
}
int main() {
  init();
  if (n >= 4 && m >= 4) {
    cout << -1 << '\n';
    return 0;
  }
  if (n <= 1 || m <= 1) {
    cout << 0 << '\n';
    return 0;
  }
  if (m < n) {
    vector<vector<char>> B;
    B.resize(m);
    for (int i = (0); i < (m); ++i)
      for (int j = (0); j < (n); ++j) B[i].push_back(A[j][i]);
    swap(n, m);
    A = B;
  }
  int total = 1 << n;
  dp.resize(m);
  for (int i = (0); i < (m); ++i) dp[i].resize(total, inf);
  for (int i = (0); i < (total); ++i) dp[m - 1][i] = dist_(m - 1, i);
  for (int i = (m - 1) - 1; i >= (0); --i) {
    for (int j = (0); j < (total); ++j) {
      int dist = dist_(i, j);
      for (int k = (0); k < (total); ++k) {
        if (check(j, k)) {
          dp[i][j] = min(dp[i][j], dp[i + 1][k]);
        }
      }
      dp[i][j] += dist;
    }
  }
  int b = inf;
  for (int i = (0); i < (total); ++i) {
    if (dp[0][i] < b) b = dp[0][i];
  }
  cout << b << '\n';
}
