#include<bits/stdc++.h>
#define maxn 500001
#define lg2 61
using namespace std;

int n;
short a[maxn][lg2];
unsigned long long niz[maxn];
unsigned long long veci[maxn];
unsigned long long manji[maxn];
unsigned long long vre[maxn][2];
short levo[maxn][lg2+1];
int maxlg;

long long precalc()
{
    for(int i=0;i<n;i++)
    {
        /*for(int j=0;j<lg2;j++)
        {
            if(j==0)
            {
                vre[i][j]=a[i][j];
            }
            else
            {
                vre[i][j]=vre[i][j-1]+a[i][j]*(1LL<<j);
            }
        }*/

        levo[i][maxlg+1]=-100;
        for(int j=maxlg;j>=0;j--)
        {
            levo[i][j]=levo[i][j+1];
            if(a[i][j]==1)levo[i][j]=j;
        }

        /*for(int j=0;j<lg2;j++)
        {
            if(levo[i][j]<0 || a[i][j]==1)
            {
                manji[i][j]=-1;
            }
            else
            {
                manji[i][j]=niz[i]-vre[i][levo[i][j]]+(1LL<<(j+1))-1;
            }
        }*/
    }

    long long res=-1;
    int curr=0;
    int prev=1;
    for(int j=0;j<=maxlg;j++)
    {
        for(int i=0;i<n;i++)
        {
            if(j==0)
            {
                vre[i][curr]=a[i][j];
            }
            else
            {
                vre[i][curr]=vre[i][prev]+a[i][j]*(1LL<<j);
            }
            if(j==0)
            {
                if(a[i][j]==1)
                {
                    veci[i]=niz[i];
                }
                else
                {
                    veci[i]=niz[i]+1;
                }
            }
            else
            {
                if(a[i][j]==1)
                {
                    veci[i]=niz[i];
                }
                else
                {
                    veci[i]=niz[i]-vre[i][prev]+(1LL<<j);
                }
            }
            if(levo[i][j]<0 || a[i][j]==1)
            {
                manji[i]=-1;
            }
            else
            {
                manji[i]=niz[i]-vre[i][curr]-(1LL<<(levo[i][j]))+(1LL<<(j+1))-1;
            }
        }

        long long tre=0;
        long long xorr=0;
        bool ext0=false;
        bool ext1=false;
        for(int i=0;i<n;i++)
        {
            long long razv=veci[i]-niz[i];
            if(manji[i]<0)
            {
                xorr^=veci[i];
                tre+=razv;
            }
            else
            {
                long long razm=niz[i]-manji[i];
                if(razv==razm)
                {
                    ext0=true;
                }
                if(abs(razv-razm)==1)
                {
                    ext1=true;
                }
                if(razv<razm)
                {
                    xorr^=veci[i];
                    tre+=razv;
                }
                else
                {
                    xorr^=manji[i];
                    tre+=razm;
                }
            }
        }

        //cout<<i<<": "<<res<<" ";

        if(xorr==0LL)
        {
            if(j==0)
            {
                if(!ext0 && !ext1)tre+=2;
                else if(ext1)tre+=1;
            }
            else
            {
                if(!ext0)tre+=1;
            }
        }
        if(tre<res || res==-1)
        {
            res=tre;
        }
        
        swap(curr,prev);
    }

    return res;
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin>>n;
    maxlg=0;
    for(int i=0;i<n;i++)
    {
        long long x;
        cin>>x;
        niz[i]=x;
        for(int j=0;j<lg2;j++)
        {
            a[i][j]=x%2;
            if(a[i][j]==1)maxlg=max(maxlg,j);
            x/=2;
        }
        //for(int j=60;j>=0;j--)cout<<a[i][j];cout<<endl;
    }

    //for(int i=0;i<n;i++)cout<<niz[i]<<" ";cout<<endl;

    //precalc();

    /*for(int i=0;i<n;i++)
    {
        cout<<niz[i]<<endl;
        for(int j=lg2-1;j>=0;j--)cout<<a[i][j];cout<<endl;///LG2 UMESTO 5
        for(int j=0;j<lg2;j++)///LG2 UMESTO 5
        {
            cout<<j<<": ";
            cout<<levo[i][j]<<" "<<manji[i][j]<<" "<<veci[i][j]<<endl;
            cout<<endl;
        }
    }*/

    /*long long res=-1;
    for(int i=0;i<=maxlg;i++)///LG2 UMESTO 5
    {
        long long tre=0;
        long long xorr=0;
        bool ext0=false;
        bool ext1=false;
        for(int j=0;j<n;j++)
        {
            long long razv=veci[j][i]-niz[j];
            if(manji[j][i]<0)
            {
                xorr^=veci[j][i];
                tre+=razv;
            }
            else
            {
                long long razm=niz[j]-manji[j][i];
                if(razv==razm)
                {
                    ext0=true;
                }
                if(abs(razv-razm)==1)
                {
                    ext1=true;
                }
                if(razv<razm)
                {
                    xorr^=veci[j][i];
                    tre+=razv;
                }
                else
                {
                    xorr^=manji[j][i];
                    tre+=razm;
                }
            }
        }

        //cout<<i<<": "<<res<<" ";

        if(xorr==0LL)
        {
            if(i==0)
            {
                if(!ext0 && !ext1)tre+=2;
                else if(ext1)tre+=1;
            }
            else
            {
                if(!ext0)tre+=1;
            }
        }
        if(tre<res || res==-1)
        {
            res=tre;
        }

        //cout<<xorr<<" "<<ext0<<" "<<tre<<" "<<res<<endl;cout<<endl;
    }*/

    cout<<precalc()<<endl;

    return 0;
}