#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <climits>
#include <iomanip>
#include <set>
#include <numeric>

#define long long long int

using namespace std;

int main() {
    ios::sync_with_stdio(false);

    int n, m; cin >> n >> m;

    vector<vector<int>> a(n, vector<int>(m));

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++)  {
            char c; cin >> c;
            a[i][j] = c == '1';
        }
    }

    if (n > 3 && m > 3) {
        cout << -1 << endl;
        return 0;
    }

    if (n > 3) {
        vector<vector<int>> aa(m, vector<int>(n));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                aa[j][i] = a[i][j];
            }
        }
        a = aa;
        swap(n, m);
    }

    int rr = INT_MAX;

    for (int e = 0; e <= 1; e++) {
        for (int change = 0; change <= 1; change++) {
            for (int change_inf = 0; change_inf <= 1; change_inf++) {
//            cout << e << " " << change << endl;
                vector<int> changed_top(m);
                for (int i = 0; i < m; i++) {
                    changed_top[i] = ((a[0][i] + a[1][i] + e) & 1) != (i & 1);
                }

                int res = accumulate(changed_top.begin(), changed_top.end(), 0);

                if (n > 2) {
                    vector<int> changed_top_inf(m);
                    vector<int> changed_bottom(m);
                    changed_bottom[0] = change;
                    res += change;

                    if (changed_top[0]) {
                        changed_top_inf[0] = change_inf;
                    }

                    for (int i = 1; i < m; i++) {
                        int ok = (a[1][i - 1] + a[1][i] + a[2][i - 1] + a[2][i] + changed_top_inf[i - 1] +
                                  changed_bottom[i - 1]) % 2 == 1;
                        if (changed_top[i]) {
                            if (!ok) {
                                changed_top_inf[i] = 1;
                            }
                        } else {
                            if (!ok) {
                                changed_bottom[i] = 1;
                            }
                        }
                    }

                    res += accumulate(changed_bottom.begin(), changed_bottom.end(), 0);
//                for (int i = 0; i < m; i++) {
//                    cout << changed_top[i] << " ";
//                }
//                cout << endl;
//                for (int i = 0; i < m; i++) {
//                    cout << changed_top_inf[i] << " ";
//                }
//                cout << endl;
//                for (int i = 0; i < m; i++) {
//                    cout << changed_bottom[i] << " ";
//                }
//                cout << endl;
//
//                cout << res << endl;
                }

                rr = min(rr, res);
            }
        }
    }

    cout << rr;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

						//watch(init);

						if(sm == 2){

				int ans = init;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

			}

			else if(sm == 3){

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			}

			mat = matcpy;

					}

				}

			}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

						//watch(init);

						if(sm == 2){

				int ans = init;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

			}

			else if(sm == 3){

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			}

			mat = matcpy;

					}

				}

			}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			if(sm == 2){

				for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

				int ans = init;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				minm = min(minm, ans);

				mat  = matcpy;

					}}

			}

			else{for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

						//watch(init);

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			mat = matcpy;

					}

				}

			}}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				cout << ans << endl;

			}

			else if(sm == 3){

				int ans = 0;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				cout << ans << endl;

			}

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			if(sm == 2){

				for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

				int ans = init;

				for(int i = 1; i <= m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				minm = min(minm, ans);

				mat  = matcpy;

					}}

			}

			else{for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					//for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							/*if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}*/

						}

						//watch(init);

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			mat = matcpy;

					}

				}

			}//}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		if(sm == m){

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < n; i++){

					int count1 = 0;

					for(int j = i; j < i + 2; j++){

						for(int k = 1; k <= 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[i + 1][2] = (mat[i + 1][2] == '1') ? '0' : '1';
						ans++;

					}

				}

				cout << ans << endl;

			}

			else if(sm == 3){

				int ans = 0;

				for(int i = 1; i < n; i++){

					int count1[2] = {0, 0};

					for(int j = i; j < i + 2; j++){

						for(int k = 1; k <= 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = i; j < i + 2; j++){

						for(int k = 2; k <= 3; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[i + 1][2] = (mat[i + 1][2] == '1') ? '0' : '1';
						ans++;

					}

					else if(count1[0] % 2 == 0){

						mat[i + 1][1] = (mat[i + 1][1] == '1') ? '0' : '1';
						ans++;

					}

					else if(count1[1] % 2 == 0){

						mat[i + 1][3] = (mat[i + 1][3] == '1') ? '0' : '1';
						ans++;

					}

				}

				cout << ans << endl;

			}

		}

		else{

			int temp;
			temp = n;
			n = m; m = temp;

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < n; i++){

					int count1 = 0;

					for(int j = i; j < i + 2; j++){

						for(int k = 1; k <= 2; k++){

							if(mat[k][j] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				cout << ans << endl;

			}

			else if(sm == 3){

				int ans = 0;

				for(int i = 1; i < n; i++){

					int count1[2] = {0, 0};

					for(int j = i; j < i + 2; j++){

						for(int k = 1; k <= 2; k++){

							if(mat[k][j] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = i; j < i + 2; j++){

						for(int k = 2; k <= 3; k++){

							if(mat[k][j] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				cout << ans << endl;

			}

		}

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			if(sm == 2){

				for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

				int ans = init;

				for(int i = 1; i <= m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				minm = min(minm, ans);

				mat  = matcpy;

					}}

			}

			else{for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					//for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							/*if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}*/

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							/*if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}*/

						}

						//watch(init);

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			mat = matcpy;

					}

				}

			}//}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

					for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

						//watch(init);

						if(sm == 2){

				int ans = init;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				minm = min(minm, ans);

			}

			else if(sm == 3){

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			}

			mat = matcpy;

					}

				}

			}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


#include<bits/stdc++.h>
using namespace std;

using ll = long long;

using pii = pair<int, int>;
using pid = pair<int, double>;
using pdd = pair<double, double>;
using pic = pair<int, char>;
using pllll = pair<ll, ll>;
using pllc = pair<ll, char>;
using pill = pair<int, ll>;
using pss = pair<string, string>;
using pis = pair<int, string>;
using plls = pair<ll, string>;
using plld = pair<ll, double>;

using vi = vector<int>;
using vd = vector<double>;
using vc = vector<char>;
using vs = vector<string>;
using vll = vector<ll>;

#define pb push_back
#define mp make_pair
#define f(i, n) for(int i = 0; i < n; i++)
#define fll(i, n) for(ll i = 0; i < n; i++)
#define F(i, n)	for(int i = 1; i <= n; i++)
#define Fll(i, n) for(ll i = 1; i <= n; i++)
#define fi first
#define se second
#define endl "\n"
#define watch(x) cout << (#x) << " is " << (x) << endl

#define fl fflush(stdout);

int main(){

	ios::sync_with_stdio(false);

	ll n, m;

	cin >> n >> m;

	vc tem(1, '-1');
	char temp;

	vector<vc> mat(n + 1, tem);

	F(i, n){

		F(j, m){

			cin >> temp;

			mat[i].pb(temp);

		}

	}

	ll sm = min(n, m);

	if(sm < 2){

		cout << 0 << endl;

	}

	else if(sm >= 4){

		cout << -1 << endl;

	}

	else{

		/*if(sm == m){

			ll count0 = 0;
			ll count1 = 0;

			ll remcount0 = 0;
			ll remcount1 = 0;

			F(i, n){

				F(j, m){

					if(!(i == 2 && j % 2 == 0) && mat[i][j] == '0'){

						count0++;

					}

					else if(!(i == 2 && j % 2 == 0) && mat[i][j] == '1'){

						count1++;

					}

					ll rem = n * m - count0 - count1;

					watch(count0);
					watch(count1);
					watch(rem);

				}

			}

			F(i, n){

				F(j, m){

					if(i == 2 && j % 2 == 0 && mat[i][j] == '0'){

						remcount0++;

					}

					else if(i == 2 && j % 2 == 0 && mat[i][j] == '1'){

						remcount1++;

					}

				}

			}

			watch(remcount0);
			watch(remcount1);

			ll ans = min(count0 + remcount1, count1 + remcount0);

			cout << ans << endl;

		}

		else if(sm == n){

		}*/

		//if(sm == n){

			int minm = INT_MAX;

			vector<vc> matcpy = mat;

			if(sm == 2){

				for(int x = 0; x < 2; x++){

				for(int y = 0; y < 2; y++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

				int ans = init;

				for(int i = 1; i <= m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

					}

				}

				minm = min(minm, ans);

				mat  = matcpy;

					}}

			}

			else{for(int x = 0; x < 2; x++){

				//for(int y = 0; y < 2; y++){

					for(int z = 0; z < 2; z++){

						int init = 0;

						for(int p = 1; p <= 3; p++){

							if(p == 1){if(mat[p][1] != x + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

							/*if(p == 2){if(mat[p][1] != y + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}*/

							if(p == 3 && n == 3){if(mat[p][1] != z + 48){

								init++;
								mat[p][1] = (mat[p][1] == '1') ? '0' : '1';

							}}

						}

						//watch(init);

				int ans = init;

				for(int i = 1; i < m; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k <= i + 1; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					/*watch(i);

					watch(count1[0]);
					watch(count1[1]);
					cout << endl;*/

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE" << endl;

					}

					else if(count1[0] % 2 == 0 && count1[1] % 2 != 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE$" << endl;

					}

					else if(count1[1] % 2 == 0 && count1[0] % 2 != 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						//cout << "HERE*" << endl;

					}

				}

				minm = min(minm, ans);

			mat = matcpy;

					}

				}

			}//}

			cout << minm << endl;

		//}

		/*else{

			if(sm == 2){

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1 = 0;

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1++;

							}

						}

					}

					if(count1 % 2 == 0){

						ans++;
						mat[2][i + 1] = (mat[2][i + 1] == 1) ? '0' : '1';

					}

				}

				cout << ans << endl;

			}

			else{

				int ans = 0;

				for(int i = 1; i < m - 1; i++){

					int count1[2] = {0, 0};

					for(int j = 1; j <= 2; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[0]++;

							}

						}

					}

					for(int j = 2; j <= 3; j++){

						for(int k = i; k < i + 2; k++){

							if(mat[j][k] == '1'){

								count1[1]++;

							}

						}

					}

					if(count1[0] % 2 == 0 && count1[1] % 2 == 0){

						mat[2][i + 1] = (mat[2][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HWER!"<< endl;

					}

					else if(count1[0] % 2 == 0){

						mat[1][i + 1] = (mat[1][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE2" << endl;

					}

					else if(count1[1] % 2 == 0){

						mat[3][i + 1] = (mat[3][i + 1] == '1') ? '0' : '1';
						ans++;

						cout << "HERE#" << endl;

					}

				}

				cout << ans << endl;

			}

		}*/

	}

	return 0;

}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, 0, sizeof(0));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, 0, sizeof(test), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                        count = 1;
                    }
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) > (*(int *)b) ? 1 : -1;
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, -1, sizeof(test));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, 0, sizeof(test), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                        count = 1;
                    }
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
int main()
{
	int n, flag, i, sum;
	int test, count;
	scanf("%d", &n);
	scanf("%d", &test);
	sum = flag = test;
	for(i = 1, count = 0; i < n; i++)
	{
		scanf("%d", &test);
		if(test = flag)
		{
			count++;
			continue;
		}
		else
		{
			while(test != flag+1&&count)
			{
				sum += !(count&1);
				count /= 2;
				flag++;
			}
			if(!count)
			{
				sum += test - flag;
				flag = test;
				count = 0;
				continue;
			}
			if(test == flag+1)
			{
				sum += !(count&1);
				count /= 2;
				flag = test;
			}
		}
	}
	while(count)
	{
		sum += !(count&1);
		count /= 2;
	}
	printf("%d\n", sum);
	return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < MAXN; i++)
            test[i] = -1;
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, n, sizeof(int), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                    }
                    count++;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 100002

int s[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) -(*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, j, count, sum;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &s[i]);
        s[i] = -1;
        qsort(s, n, sizeof(int), cmp);
        for(i = j = count = sum = 0; i < n && j <= s[n-1]; j++)
        {
            if(s[i] == j)
            {
                count++;
            }
            else
            {
                if(count)
                    count = (count-1)/2;
                else
                {
                    sum += s[i]-j;
                    j = s[i]-1;
                }

                if(j+1 > s[i])
                    i++;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 100002

int s[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) -(*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, j, count, sum;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &s[i]);
        s[i] = -1;
        qsort(s, n, sizeof(int), cmp);
        for(i = j = count = sum = 0; i < n && j <= s[n-1]; j++)
        {
            if(s[i] == j)
            {
                count++;
            }
            else
            {
                if(count)
                    count = (count-1)/2;
                else
                {
                    sum += s[i]-j;
                    j = s[i]-1;
                }
                if(j+1 > s[i])
                {
                    i++;    j--;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, -1, sizeof(test));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, n, sizeof(int), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                        count = 1;
                    }
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, 0, sizeof(0));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, 0, sizeof(test), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
                continue;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                    }
                }
            }
        }
//        while(count)
//        {
//            if(count&1)
//            {
//                count = (count - 1)>>1;
//                t++;
//            }
//            else
//            {
//                count = count>>1;
//                t++;
//                sum++;
//            }
//        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, -1, sizeof(test));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, n, sizeof(int), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                    }
                    count++;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < MAXN; i++)
            test[i] = -1;
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, n, sizeof(int), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                        count = 1;
                    }
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 100002

int s[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) -(*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, j, count, sum;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &s[i]);
        s[i] = -1;
        qsort(s, n, sizeof(int), cmp);
        for(i = j = count = sum = 0; i < n && j <= s[n-1]; j++)
        {
            if(s[i] == j)
            {
                count++;
            }
            else
            {
                if(count)
                    count = (count-1)/2;
                else
                    sum++;
                if(j+1 > s[i])
                    i++;
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 100002

int s[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) -(*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, j, count, sum;
    while(~scanf("%d", &n))
    {
        for(i = 0; i < n; i++)
            scanf("%d", &s[i]);
        s[i] = -1;
        qsort(s, n, sizeof(int), cmp);
        for(i = j = count = sum = 0; i < n && j <= s[n-1]; j++)
        {
            if(s[i] == j)
            {
                i++;
                j--;
                count++;
                continue;
            }
            else
            {
                if(count)
                    count = (count-1)/2;
                else
                {
                    sum += s[i]-j;
                    j = s[i]-1;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
# include <stdbool.h>

# define MAXN 111111

int test[MAXN];

int cmp(const void *a, const void *b)
{
    return (*(int *)a) - (*(int *)b);
}

int main()
{
//    freopen("in", "r", stdin);
//    freopen("out", "w", stdout);
    int n, i, count, sum, t;
    while(~scanf("%d", &n))
    {
        memset(test, -1, sizeof(test));
        for(i = 0 ; i < n; i++)
            scanf("%d", &test[i]);
        qsort(test, 0, sizeof(test), cmp);
        sum += test[0] - 0;
        for(i = t = 0, count = 1; i <= n; i++)
        {
            if(test[i] == test[i+1])
            {
                count++;
            }
            else
            {
                if(count == 1 && test[i+1] > test[i])
                {
                    sum += test[i+1]- test[i] - 1;
                }
                else
                {
                    t = test[i];
                    while(count)
                    {
                        if(count&1)
                        {
                            count = (count - 1)>>1;
                            t++;
                        }
                        else
                        {
                            count = count>>1;
                            t++;
                            sum++;
                        }
                        if(t == test[i+1])
                        {
                            break;
                        }
                    }
                    if(!count && i < n - 1)
                    {
                        sum += test[i+1] - t;
                        count = 1;
                    }
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>m;
map<ll,ll>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        ll val;cin>>val;
        m[val]++;
    }
    for(it=m.begin();it!=m.end();it++)
    {
        ll val=it->first;
        if(m[val]%2==0)
        {
            m[val+1]+=(m[val]/2);
            m[val]=0;
        }
        else
        {
            m[val+1]+=(m[val]/2);
            m[val]=1;
        }
    }
    it=m.begin();ll ans=0;
    if(it->second==1)ans+=(it->first);
    else ans+=(it->first+1);
    ll last=it->first;it++;
    while(it!=m.end())
    {
        ll val=it->first;
        if(m[val]==1)
        {
            ans+=(val-last-1);
            last=val;
        }
        else
        {
            ans+=(val-last);last=val;
        }
        it++;
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>m;
map<ll,ll>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        ll val;cin>>val;
        m[val]++;
        if(m[val]==2)
        {
            m[val]=0;
            m[val+1]+=1;
        }
    }
    it=m.begin();ll ans=0;
    if(it->second==1)ans+=(it->first);
    else ans+=(it->first+1);
    ll last=it->first;it++;
    while(it!=m.end())
    {
        ll val=it->first;
        if(m[val]==1)
        {
            ans+=(val-last-1);
            last=val;
        }
        else
        {
            ans+=(val-last);last=val;
        }
        it++;
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>m;
map<ll,ll>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        ll val;cin>>val;
        m[val]++;
        if(m[val]%2==0)
        {
            if(m[val]!=0)
            m[val+1]+=(m[val]/2);
            m[val]=0;
        }
        else
        {
            if(m[val]!=1)
            m[val+1]+=(m[val]/2);
            m[val]=1;
        }
    }
    it=m.begin();ll ans=0;
    if(it->second==1)ans+=(it->first);
    else ans+=(it->first+1);
    ll last=it->first;it++;
    while(it!=m.end())
    {
        ll val=it->first;
        if(m[val]==1)
        {
            ans+=(val-last-1);
            last=val;
        }
        else
        {
            ans+=(val-last);last=val;
        }
        it++;
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>m;
map<ll,ll>::iterator it;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;cin>>n;
    for(int i=0;i<n;i++)
    {
        ll val;cin>>val;
        m[val]++;
    }
    for(it=m.begin();it!=m.end();it++)
    {
        ll val=it->first;
        if(m[val]%2==0)
        {
            if(m[val]!=0)
            m[val+1]+=(m[val]/2);
            m[val]=0;
        }
        else
        {
            if(m[val]!=1)
            m[val+1]+=(m[val]/2);
            m[val]=1;
        }
        //cout<<val<<"\n";
    }
    it=m.begin();ll ans=0;
    if(it->second==1)ans+=(it->first);
    else ans+=(it->first+1);
    ll last=it->first;it++;
    while(it!=m.end())
    {
        ll val=it->first;
        if(m[val]==1)
        {
            ans+=(val-last-1);
            last=val;
        }
        else
        {
            ans+=(val-last);last=val;
        }
        it++;
    }
    cout<<ans<<"\n";
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int res,n,t,a[100010];

int main(){
  cin>>n;
  rep(i,n)cin>>t,a[t]++;
  rep(i,100010){
    if(a[i]>1){
      a[i+1] += a[i]/2;
      a[i] = a[i]%2;
    }
  }
  t = 0;
  for(int i=100010;i>=0;i--){
    if(t==1 && a[i]==0)res++;
    else if(t==0 && a[i]==1)t=1;
  }
  cout<<res<<endl;

}


#include <iostream>
#include <cmath>

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int res,n,t,p,c,a[100010],cur;

int main(){
  cin>>n;
  rep(i,n)cin>>a[i];
  a[n] = (int)2e9+10000;

  res = a[0];
  cur = a[0];

  while(1){
    while(p<n && a[p]==cur){p++;c++;}
    while(cur<a[p] && c){
      if(!c%2)res++;
      c/=2;
      cur++;
    }
    if(p==n)break;
    res += a[p]-cur;
  }

  cout<<res<<endl;

}


#include <iostream>
#include <cmath>

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int res,n,t,p,c,a[100010],cur;

int main(){
  cin>>n;
  rep(i,n)cin>>a[i];
  a[n] = (int)2e9+10000;

  res = a[0];
  cur = a[0];

  while(1){
    while(p<n && a[p]==cur){p++;c++;}
    while(cur<a[p] && c){
      if(!c%2)res++;
      c/=2;
      cur++;
    }
    if(p==n)break;
    res += a[p]-cur;
    cur = a[p];
  }

  cout<<res<<endl;

}


#include <iostream>
#include <algorithm>
#include <cmath>

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int res,n,t,a[110000];

int main(){
  cin>>n;
  rep(i,n)cin>>t,a[t]++;
  rep(i,101000){
    if(a[i]>1){
      a[i+1] += a[i]/2;
      a[i] = a[i]%2;
    }
  }
  t = 0;
  for(int i=101000;i>=0;i--){
    if(t==1 && a[i]==0)res++;
    else if(t==0 && a[i]==1)t=1;
  }
  cout<<res<<endl;

}


#include <iostream>
#include <cmath>

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

int res,n,t,p,c,a[100010],cur;

int main(){
  cin>>n;
  rep(i,n)cin>>a[i];
  a[n] = (int)2e9+10000;

  res = a[0];
  cur = a[0];

  while(1){
    while(p<n && a[p]==cur){p++;c++;}
    while(cur<a[p] && c){
      if(c%2==0)res++;
      c/=2;
      cur++;
    }
    if(p==n)break;
    res += a[p]-cur;
    cur = a[p];
  }

  cout<<res<<endl;

}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long double ld;

const ld E = 0.000000000000000001;

#define pb push_back
#define sz(a) (int)a.size()
#define file freopen("input.txt", "r", stdin) //freopen("output.txt", "w", stdout)

int main(){
    //file;
    int n;
    cin >> n;
    vector <ll> in;
    ll a;
    for (int i = 0; i < n; ++i){
        cin >> a;
        in.pb(a);
        while (sz(in) > 1 && in[sz(in)-2] == in[sz(in) - 1]){
            ll s1 = in[sz(in) - 1];
            in.pop_back();
            in.pop_back();
            in.pb(s1 + 1);
        }
    }
    sort(in.begin(), in.end());
    ll kol = 0;
    ll now = 0;
    for (int i = 0; i < sz(in); ++i){
        kol += in[i] - now;
        now = in[i] + 1;
    }
    cout << kol;
}


#include "iostream"
#include <vector>
#include <string>
#include <cmath>
#include <cstdio>
#include "time.h"

#define ll long long
#define pi pait <int, int>
#define vi vector <int>
#define pb push_back
#define sz(a) (int)a.size()
#define ld long double

using namespace std;

int mas[1000000];

void ur(int st);

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i){
        int st;
        cin >> st;
        ur(st);
    }
    int pos_st = 0;
    int koli = 0;
    for (int i = 0; i < 10000000; ++i){
        if (mas[i] > 0) pos_st = i;
    }
    for (int i = 0; i < pos_st; ++i){
        if (mas[i] == 0) koli++;
    }
    cout << koli;
}

void ur(int st){
    mas[st]++;
    int i = st;
    while (mas[i] >= 2){
        mas[i] -= 2;
        ++i;
        mas[i]++;
    }
}


#include "iostream"
#include <vector>
#include <string>
#include <cmath>
#include <cstdio>
#include "time.h"

#define ll long long
#define pi pait <int, int>
#define vi vector <int>
#define pb push_back
#define sz(a) (int)a.size()
#define ld long double

using namespace std;

int mas[2*1000*1000*1000];

void ur(int st);

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i){
        int st;
        cin >> st;
        ur(st);
    }
    int pos_st = 0;
    int koli = 0;
    for (int i = 0; i < 10000000; ++i){
        if (mas[i] > 0) pos_st = i;
    }
    for (int i = 0; i < pos_st; ++i){
        if (mas[i] == 0) koli++;
    }
    cout << koli;
}

void ur(int st){
    mas[st]++;
    int i = st;
    while (mas[i] >= 2){
        mas[i] -= 2;
        ++i;
        mas[i]++;
    }
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long double ld;

const ld E = 0.000000000000000001;

#define pb push_back
#define sz(a) (int)a.size()
#define file freopen("input.txt", "r", stdin) //freopen("output.txt", "w", stdout)

int main(){
    int n;
    cin >> n;
    vector <ll> in;
    for (int i = 0; i < n; ++i){
        ll a;
        cin >> a;
        if (sz(in) > 0 && in[sz(in)-1] == a){
            in.pop_back();
            in.pb(a+1);
        }
        else if (sz(in) > 0 && in[sz(in) - 1] > a){
            ld s = in[sz(in) - 1];
            in.pop_back();
            in.pb(a);
            in.pb(s);
        }
        else{
            in.pb(a);
        }
    }
    ll kol = 0;
    ll now = 0;
    for (int i = 0; i < sz(in); ++i){
        kol += in[i] - now;
        now = in[i] + 1;
    }
    cout << kol;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long double ld;

const ld E = 0.000000000000000001;

#define pb push_back
#define sz(a) (int)a.size()
#define file freopen("input.txt", "r", stdin) //freopen("output.txt", "w", stdout)

int main(){
    int n;
    cin >> n;
    vector <ll> in;
    vector <ll> brothers;
    ll last = -1;
    ll a;
    cin >> a;
    brothers.pb(a);
    last = a;
    for (int i = 1; i < n; ++i){
        cin >> a;
        if (a == last){
            brothers.pb(a);
            while (sz(brothers) > 1 && brothers[sz(brothers)-2] == brothers[sz(brothers) - 1]){
                ll s1 = brothers[sz(brothers) - 1];
                brothers.pop_back();
                brothers.pop_back();
                brothers.pb(s1 + 1);
            }
        }
        else{
            reverse(brothers.begin(), brothers.end());
            for (int j = 0 ; j < sz(brothers); ++j){
                in.pb(brothers[j]);
            }
            brothers.clear();
            last = a;
            brothers.pb(a);
        }
    }
    reverse(brothers.begin(), brothers.end());
    for (int j = 0 ; j < sz(brothers); ++j){
        in.pb(brothers[j]);
    }
    ll kol = 0;
    ll now = 0;
    for (int i = 0; i < sz(in); ++i){
        kol += in[i] - now;
        now = in[i] + 1;
    }
    cout << kol;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef long double ld;

const ld E = 0.000000000000000001;

#define pb push_back
#define sz(a) (int)a.size()
#define file freopen("input.txt", "r", stdin) //freopen("output.txt", "w", stdout)

int main(){
    file;
    int n;
    cin >> n;
    vector <ll> in;
    ll a;
    for (int i = 0; i < n; ++i){
        cin >> a;
        in.pb(a);
        while (sz(in) > 1 && in[sz(in)-2] == in[sz(in) - 1]){
            ll s1 = in[sz(in) - 1];
            in.pop_back();
            in.pop_back();
            in.pb(s1 + 1);
        }
    }
    sort(in.begin(), in.end());
    ll kol = 0;
    ll now = 0;
    for (int i = 0; i < sz(in); ++i){
        kol += in[i] - now;
        now = in[i] + 1;
    }
    cout << kol;
}


#include "iostream"
#include <vector>
#include <string>
#include <cmath>
#include <cstdio>
#include "time.h"

#define ll long long
#define pi pait <int, int>
#define vi vector <int>
#define pb push_back
#define sz(a) (int)a.size()
#define ld long double

using namespace std;

int mas[10000000];

void ur(int st);

int main(){
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i){
        int st;
        cin >> st;
        ur(st);
    }
    int pos_st = 0;
    int koli = 0;
    for (int i = 0; i < 10000000; ++i){
        if (mas[i] > 0) pos_st = i;
    }
    for (int i = 0; i < pos_st; ++i){
        if (mas[i] == 0) koli++;
    }
    cout << koli;
}

void ur(int st){
    mas[st]++;
    int i = st;
    while (mas[i] >= 2){
        mas[i] -= 2;
        ++i;
        mas[i]++;
    }
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

map < ll, ll > mp;

ll n, ans;

ll arr[N];

int main() {

	cin >> n;

	for (ll i = 1; i <= n; i++) {
		cin >> arr[i];
		mp[arr[i]]++;
	}

	for (int i = 1; i <= n; i++) {
		if (mp[arr[i]] > 1) {
			ll temp = mp[arr[i]];
			mp[arr[i]] = temp & 1;
			mp[arr[i] + temp >> 1]++;
		}
	}

	map < ll, ll > :: iterator it, temp;

	bool flag = false;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> first != 0 and mp[it -> first - 1] == false) {
			ans += (it -> first) - ((flag == false) ? 0 : (temp -> first + 1));
		}
		temp = it; flag = true;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

map < ll, ll > mp;

ll n, ans, a;

bool flag;

int main() {

	cin >> n;

	for (ll i = 1; i <= n; i++) {
		cin >> a;
		mp[a]++;
	}

	map < ll, ll > :: iterator it, temp;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> second > 1) {
			ll temp = it -> second;
			it -> second = temp & 1;
			mp[it -> first + 1] += temp >> 1;
		}
	}

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> second == false) continue;
		if (it -> first != 0 and mp[it -> first - 1] == false)
			ans += (it -> first) - ((flag == false) ? 0 : (temp -> first + 1));
		temp = it; flag = true;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

map < ll, ll > mp;

int n, ans;

ll arr[N];

int main() {

	cin >> n;

	for (int i = 1; i <= n; i++) {
		cin >> arr[i];
		mp[arr[i]]++;
	}

	for (int i = 1; i <= n; i++) {
		if (mp[arr[i]] > 1) {
			int temp = mp[arr[i]];
			mp[arr[i]] = temp & 1;
			mp[arr[i] + temp >> 1]++;
		}
	}

	map < ll, ll > :: iterator it, temp;

	bool flag = false;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> first != 0 and mp[it -> first - 1] == false) {
			ans += (it -> first) - ((flag == false) ? 0 : temp -> first);
		}
		temp = it; flag = true;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

map < ll, ll > mp;

ll n, ans;

ll arr[N];

int main() {

	cin >> n;

	for (ll i = 1; i <= n; i++) {
		cin >> arr[i];
		mp[arr[i]]++;
	}

	for (int i = 1; i <= n; i++) {
		if (mp[arr[i]] > 1) {
			ll temp = mp[arr[i]];
			mp[arr[i]] = temp & 1;
			mp[arr[i] + temp >> 1]++;
		}
	}

	map < ll, ll > :: iterator it, temp;

	bool flag = false;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> first != 0 and mp[it -> first - 1] == false) {
			ans += (it -> first) - ((flag == false) ? 0 : temp -> first);
		}
		temp = it; flag = true;
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e5 + 9;

map < ll, ll > mp;

ll n, ans;

ll arr[N];

int main() {

	cin >> n;

	for (ll i = 1; i <= n; i++) {
		cin >> arr[i];
		mp[arr[i]]++;
	}

	map < ll, ll > :: iterator it;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> second > 1) {
			ll temp = it -> second;
			ll temp2 = it -> first;
			it -> second = temp & 1;
			mp[temp2 + 1] += temp >> 1;
		}
	}

	map < ll, ll > :: iterator temp;

	bool flag = false;

	for (it = mp.begin(); it != mp.end(); it++) {
		if (it -> second == false) continue;
	//	cout << it -> first << " " << it -> second << endl;
		if (it -> first != 0 and mp[it -> first - 1] == false) {
		//	cout << 123 << endl;
			ans += (it -> first) - ((flag == false) ? 0 : (temp -> first + 1));
		}
		temp = it; flag = true;
	}

	cout << ans << endl;

	return 0;
}

/*

26
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2

*/


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		ll a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	map<int,int>::iterator it = cnt.begin();
	for (; it != cnt.end(); it++ ){
		int an = (*it).first;
		int cn = (*it).second;
		if (cn > 1 ){
			cnt[an]   -= (cn-1);
			cnt[an+1] += (cn-1)/2;
		} // end if
	} // end for
	int max_a = 0;
	vector<int> arr; arr.clear();
	for (it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		int a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	map<int,int>::iterator it = cnt.begin();
	for (; it != cnt.end(); it++ ){
		int an = (*it).first;
		int cn = (*it).second;
		if (cn > 1 ){
			cnt[an]   -= (cn-1);
			cnt[an+1] += (cn-1)/2;
		} // end if
	} // end for
	int max_a = 0;
	vector<int> arr; arr.clear();
	for (it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;

	int max_a = -1;
	set<int> p; p.clear();
	rep (i, n ){
		int a;
		cin >> a;
		if (!p.count (a ) ){
			p.insert (a );
			max_a = max (max_a, a );
			continue;
		} // end if
		while (p.count (a ) ){
			p.erase (a );
			a++;
		} // end while
		p.insert (a );
		max_a = max (max_a , a );
	} // end rep
	cout << max_a - p.size() + 1 << endl;

#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<ll,int> cnt; cnt.clear();
	ll sum = 0LL;
	rep (i, n ){
		ll a;
		cin >> a;
		sum += a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	map<ll,int>::iterator it = cnt.begin();
	for (; it != cnt.end(); it++ ){
		ll an = (*it).first;
		int cn = (*it).second;
		if (cn > 1 ){
			cnt[an]   -= (cn-1);
			cnt[an+1LL] += (cn-1)/2;
		} // end if
	} // end for
	vector<ll> arr; arr.clear();
	for (it = cnt.begin(); it != cnt.end(); it++ ){
		arr.push_back ((*it).first );
	} // end for
	vector<ll>::iterator jt = lower_bound (ALL (arr), sum );
	int ind = (jt - arr.begin());
	cout << sum - ind << endl;
//	cout << "ind: " << ind << endl;
//		cout << "an: " << (*it).first << " cnt: " << (*it).second << endl;
//	} // end for
//	cout << sum - (ll)cnt.size() << endl;

#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;

	int max_a = -1;
	set<int> p; p.clear();
	rep (i, n ){
		int a;
		cin >> a;
		if (p.count (a ) ){
			p.erase (a );
			p.insert (a+1);
			max_a = max (max_a, a+1 );
		}else{
			p.insert (a );
			max_a = max (max_a, a );
		} // end if
	} // end rep
	cout << max_a - p.size() + 1 << endl;

#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit
#include <cstdio>	// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream
#include <cstring>	// require memset
#include <cctype>	// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define DEBUG 0

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.C";
string NAME;
string itos (int n )
{
	stringstream ss;
	ss << n;

	return ss.str();
}

int main()
{
//	cut here before submit
#if DEBUG
	NAME = FILE_NAME;
	int CNT = 1;
	NAME += itos (CNT );
	while (freopen (NAME.c_str() , "r", stdin ) != NULL ) {
#endif
	int n;
	cin >> n;
	map<int,int> cnt; cnt.clear();
	rep (i, n ){
		int a;
		cin >> a;
		cnt[a]++;
	} // end rep
//	cerr << "sum: " << sum << endl;
	while (true ){
		bool found = false;
		map<int,int>::iterator it = cnt.begin();
		for (; it != cnt.end(); it++ ){
			int an = (*it).first;
			int cn = (*it).second;
			if (cn >= 2 ){
				cnt[an]   -= cn;
				cnt[an+1] += cn/2;
				found |= true;
			} // end if
		} // end for
		if (!found ) break;
	} // end while

	int max_a = 0;
	vector<int> arr; arr.clear();
	for (map<int,int>::iterator it = cnt.begin(); it != cnt.end(); it++ ){
		int ca = (*it).first;
		arr.push_back (ca );
		max_a = ca;
	} // end for
	cout << max_a - arr.size() + 1 << endl;
#if DEBUG
	CNT++;	// cut here before submit
	NAME = FILE_NAME;
	NAME += itos (CNT );
	} // end loop; cut here before submit
#endif
	return 0;
}