//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 3e7+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 6e6+10;

struct trie
{
    set<int> nis;
    int par[N], ch[N][2];

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        for(int i = 2; i < N; i++) nis.insert(i);
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                int last = *nis.begin();
                ch[v][X] = last;
                nis.erase(last);
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            nis.insert(v);
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 2e7+10;

struct trie
{
    int ch[N][2], last;
    vector<int> leaf[maxn];

    trie()
    {
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            v = ch[v][X];
        }

        v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            leaf[v].push_back(x);
            v = ch[v][X];
        }
        leaf[v].push_back(x);
    }

    int get(int x, int v, int h)
    {
        ll ans = 0;
        for(int i = h; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }

    int get_ans(int v, int h)
    {
        if(ch[v][0] == 0 && ch[v][1] == 0) return 0;
        if(ch[v][0] == 0) return get_ans(ch[v][1],h-1);
        if(ch[v][1] == 0) return get_ans(ch[v][0],h-1);

        int ans = 0;
        ans += get_ans(ch[v][1],h-1);
        ans += get_ans(ch[v][0],h-1);

        int mn = inf;
        for(auto x : leaf[ch[v][1]])
            mn = min(mn, get(x,ch[v][0],h-1));

        return ans + mn + (1<<h);
    }
} trie;

int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        trie.add(x);
    }
    cout<< trie.get_ans(1,30);
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 1e7+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int par[maxn];
int get_par(int v)
{
    if(par[v] == v) return v;
    return par[v] = get_par(par[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    par[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

struct node
{
    node *ch[2], *par;

    node()
    {
        ch[0] = ch[1] = NULL;
        par = NULL;
    }
};

struct trie
{
    node *root;

    trie()
    {
        root = new node;
    }

    void add(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(v->ch[X] == NULL)
                v->ch[X] = new node;
            (v->ch[X])->par = v;
            v = v->ch[X];
        }
    }

    void del(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = v->ch[X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            node *u = v->par;
            u->ch[X] = NULL;
            if(u->ch[!X] != NULL) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(v->ch[X] != NULL)
                v = v->ch[X];
            else
            {
                v = v->ch[!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            par[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int par[maxn], cur;
int get_par(int v)
{
    if(par[v] == v) return v;
    return par[v] = get_par(par[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    par[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
    cur = u;
}

struct node
{
    node *ch[2], *par;

    node()
    {
        ch[0] = ch[1] = NULL;
        par = NULL;
    }
};

struct trie
{
    node *root;

    trie()
    {
        root = new node;
    }

    void add(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(v->ch[X] == NULL)
                v->ch[X] = new node;
            (v->ch[X])->par = v;
            v = v->ch[X];
        }
    }

    void del(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = v->ch[X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            node *u = v->par;
            u->ch[X] = NULL;
            if(u->ch[!X] != NULL) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(v->ch[X] != NULL)
                v = v->ch[X];
            else
            {
                v = v->ch[!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            par[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);

        comp[cur].clear();
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 2e7+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second
#define int long long

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 6e6+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second
#define int long long

const int maxn = 2e5+10;
const ll inf = 1e18;

int par[maxn];
int get_par(int v)
{
    if(par[v] == v) return v;
    return par[v] = get_par(par[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    par[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

struct node
{
    node *ch[2], *par;

    node()
    {
        ch[0] = ch[1] = NULL;
        par = NULL;
    }
};

struct trie
{
    node *root;

    trie()
    {
        root = new node;
    }

    void add(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(v->ch[X] == NULL)
                v->ch[X] = new node;
            (v->ch[X])->par = v;
            v = v->ch[X];
        }
    }

    void del(int x)
    {
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = v->ch[X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            node *u = v->par;
            u->ch[X] = NULL;
            if(u->ch[!X] != NULL) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        node *v = root;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(v->ch[X] != NULL)
                v = v->ch[X];
            else
            {
                v = v->ch[!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            par[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

//const int maxn = 2e5+10;
const ll inf = 1e9+10;

const int N = 6e6+10;

struct trie
{
    int ch[N][2], last;
    vector<int> leaf[N];

    trie()
    {
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            v = ch[v][X];
        }

        v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            leaf[v].push_back(x);
            v = ch[v][X];
        }
        leaf[v].push_back(x);
    }

    int get(int x, int v, int h)
    {
        ll ans = 0;
        for(int i = h; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }

    ll get_ans(int v, int h)
    {
        if(ch[v][0] == 0 && ch[v][1] == 0) return 0;
        if(ch[v][0] == 0) return get_ans(ch[v][1],h-1);
        if(ch[v][1] == 0) return get_ans(ch[v][0],h-1);

        ll ans = 0;
        ans += get_ans(ch[v][1],h-1);
        ans += get_ans(ch[v][0],h-1);

        int mn = inf;
        for(auto x : leaf[ch[v][1]])
            mn = min(mn, get(x,ch[v][0],h-1));

        return ans + mn + (1<<h);
    }
} trie;

int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        trie.add(x);
    }
    cout<< trie.get_ans(1,30);
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 5e7+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


//be name khoda
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

#define F first
#define S second

const int maxn = 2e5+10;
const ll inf = 1e9+10;

int pa[maxn];
int get_par(int v)
{
    if(pa[v] == v) return v;
    return pa[v] = get_par(pa[v]);
}

set<pair<int,int>> s;
vector<int> comp[maxn];
void dsu(int u, int v)
{
    u = get_par(u);
    v = get_par(v);

    if(u == v) return;

    if(comp[u].size() > comp[v].size()) swap(u,v);

    s.erase({comp[u].size(),u});
    s.erase({comp[v].size(),v});

    pa[u] = v;
    for(auto x : comp[u])
        comp[v].push_back(x);
    s.insert({comp[v].size(),v});
}

const int N = 2.7e7+10;

struct trie
{
    int par[N], ch[N][2], last;

    trie()
    {
        memset(par, 0, sizeof par);
        memset(ch, 0, sizeof ch);
        last = 1;
    }

    void add(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            if(ch[v][X] == 0)
            {
                last++;
                ch[v][X] = last;
            }

            par[ch[v][X]] = v;
            v = ch[v][X];
        }
    }

    void del(int x)
    {
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);
            v = ch[v][X];
        }

        for(int i = 0; i <= 30; i++)
        {
            bool X = x & (1<<i);
            int u = par[v];
            ch[u][X] = 0;
            if(ch[u][!X] != 0) break;
            v = u;
        }
    }

    int get(int x)
    {
        ll ans = 0;
        int v = 1;
        for(int i = 30; i >= 0; i--)
        {
            bool X = x & (1<<i);

            if(ch[v][X] != 0)
                v = ch[v][X];
            else
            {
                v = ch[v][!X];
                ans += (1<<i);
            }
        }
        return ans;
    }
} trie;

map<int,int> mp;
int rmp[maxn];
int n;

signed main()
{
    cin>> n;
    for(int i = 1, x; i <= n; i++)
    {
        cin>> x;
        if(!mp.count(x))
        {
            mp.insert({x,i});
            rmp[i] = x;
            s.insert({1,i});
            pa[i] = i;
            comp[i].push_back(i);
            trie.add(x);
        }
    }

    ll ans = 0;
    while(s.size() > 1)
    {
        int v = s.begin()->second;
        for(auto x : comp[v])
            trie.del(rmp[x]);

        pair<int,int> mn = {inf,inf};
        for(auto x : comp[v])
        {
            int X = trie.get(rmp[x]);
            if(X <= mn.F)
                mn = {X,x};
        }

        int X = mn.F, x = mn.S;
        ans += X;
        dsu(x,mp[(rmp[x]^X)]);

        for(auto x : comp[v])
            trie.add(rmp[x]);
    }
    cout<< ans;
}


#pragma GCC optimize("O3");

#include <iostream>
#include <string.h>
#include <vector>

using namespace std;

typedef long long lli;

int L[3000000], R[3000000], m;

void ins(int x) {
	int cur=1;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[cur]) R[cur]=m++;
			cur=R[cur];
		} else {
			if(!L[cur]) L[cur]=m++;
			cur=L[cur];
		}
	}
}

int qry(int x) {
	int cur=1, ret=0;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(R[cur]) cur=R[cur];
			else {
				ret|=(1<<i);
				cur=L[cur];
			}
		} else {
			if(L[cur]) cur=L[cur];
			else {
				ret|=(1<<i);
				cur=R[cur];
			}
		}
	}
	return ret;
}

int n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.size()>r.size()) swap(l,r);
	if(l.empty() || r.empty()) {
		v=vector<int>();
		solve(l,b-1); solve(r,b-1);
		return;
	}
	for(int i=1;i<m;i++)
		L[i]=R[i]=0;
	m=1;
	int root=m++;
	for(int i=0;i<l.size();i++)
		ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	v=vector<int>();
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.empty() || r.empty()) {
		solve(l,b-1); solve(r,b-1);
		return;
	}
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	int msz=0;
	for(int i=1;i<=n;i++) {
		if(con[i].size()>msz) msz=con[i].size();
	}
	lli ret=0;
	bool fst=true;
	for(int i=1;i<con.size();i++) if(!con[i].empty() && !mark[i]) {
		if(fst && con[i].size()==msz) {
			fst=false; continue;
		}
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
	void x() {
		for(int i=0;i<2;i++) {
			if(nxt[i]!=NULL) delete nxt[i];
		}
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.size()>r.size()) swap(l,r);
	if(l.empty() || r.empty()) {
		solve(l,b-1); solve(r,b-1);
		return;
	}
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	root->x();
	delete root;
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6000000], R[6000000], cnt[6000000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		int l=L[u], r=R[u];
		if(x&(1<<i)) {
			if(!r) {
				R[u]=m++;
				r=m-1;
			}
			cnt[r]++;
			u=r;
		} else {
			if(!l) {
				L[u]=m++;
				l=m-1;
			}
			cnt[l]++;
			u=l;
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		int l=L[u], r=R[u];
		if(x&(1<<i)) {
			cnt[r]--;
			u=r;
		} else {
			cnt[l]--;
			u=l;
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	lli ret=0;
	for(int i=1;i<=n;i++) if(!con[i].empty() && !mark[i]) {
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].rbegin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <iostream>
#include <string.h>
#include <vector>

using namespace std;

typedef long long lli;

int L[3000000], R[3000000], m;

void ins(int x) {
	int cur=1;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[cur]) R[cur]=m++;
			cur=R[cur];
		} else {
			if(!L[cur]) L[cur]=m++;
			cur=L[cur];
		}
	}
}

int qry(int x) {
	int cur=1, ret=0;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(R[cur]) cur=R[cur];
			else {
				ret|=(1<<i);
				cur=L[cur];
			}
		} else {
			if(L[cur]) cur=L[cur];
			else {
				ret|=(1<<i);
				cur=R[cur];
			}
		}
	}
	return ret;
}

int n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.size()>r.size()) swap(l,r);
	if(l.empty() || r.empty()) {
		v=vector<int>();
		solve(l,b-1); solve(r,b-1);
		return;
	}
	for(int i=1;i<m;i++)
		L[i]=R[i]=0;
	m=1;
	int root=m++;
	for(int i=0;i<l.size();i++)
		ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	v=vector<int>();
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#pragma GCC optimize("O3")

#include <iostream>
#include <string.h>
#include <vector>

using namespace std;

typedef long long lli;

int L[3000000], R[3000000], m;

void ins(int x) {
	int cur=1;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[cur]) R[cur]=m++;
			cur=R[cur];
		} else {
			if(!L[cur]) L[cur]=m++;
			cur=L[cur];
		}
	}
}

int qry(int x) {
	int cur=1, ret=0;
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(R[cur]) cur=R[cur];
			else {
				ret|=(1<<i);
				cur=L[cur];
			}
		} else {
			if(L[cur]) cur=L[cur];
			else {
				ret|=(1<<i);
				cur=R[cur];
			}
		}
	}
	return ret;
}

int n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.size()>r.size()) swap(l,r);
	if(l.empty() || r.empty()) {
		v=vector<int>();
		solve(l,b-1); solve(r,b-1);
		return;
	}
	for(int i=1;i<m;i++)
		L[i]=R[i]=0;
	m=1;
	int root=m++;
	for(int i=0;i<l.size();i++)
		ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	v=vector<int>();
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

vector<int> con[200001];
bool vis[200001];
int cc[200001], cn, q[200001], b, e;

lli bv() {
	memset(vis,0,sizeof(vis));
	lli ret=0;
	for(int i=1;i<=n;i++) if(!vis[fd(i)]) {
		vis[i]=true;
		b=e=cn=0;
		q[e++]=i;
		while(b<e) {
			int u=q[b++];
			cc[cn++]=u;
			for(int i=0;i<con[u].size();i++) {
				int v=con[u][i];
				if(!vis[v]) {
					vis[v]=true;
					q[e++]=v;
				}
			}
		}
		if(cn==n) return -1;
		for(int i=0;i<cn;i++) {
			int u=cc[i];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int i=0;i<cn;i++) {
			int u=cc[i], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		un(i,idx);
		con[i].push_back(idx);
		con[idx].push_back(i);
		for(int i=0;i<cn;i++) {
			int u=cc[i];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6000000], R[6000000], cnt[6000000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	lli ret=0;
	for(int i=1;i<=n;i++) if(!con[i].empty() && !mark[i]) {
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6000000], R[6000000], cnt[6000000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		int l=L[u], r=R[u];
		if(x&(1<<i)) {
			if(!r) {
				R[u]=m++;
				r=m-1;
			}
			cnt[r]++;
			u=r;
		} else {
			if(!l) {
				L[u]=m++;
				l=m-1;
			}
			cnt[l]++;
			u=l;
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		int l=L[u], r=R[u];
		if(x&(1<<i)) {
			cnt[r]--;
			u=r;
		} else {
			cnt[l]--;
			u=l;
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	lli ret=0;
	for(int i=1;i<=n;i++) if(!con[i].empty() && !mark[i]) {
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	lli ret=0;
	for(int i=1;i<con.size();i++) if(!con[i].empty() && !mark[i]) {
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

vector<int> con[200001];
bool vis[200001];
int cc[200001], cn;

lli bv() {
	memset(vis,0,sizeof(vis));
	lli ret=0;
	for(int i=1;i<=n;i++) if(!vis[fd(i)]) {
		vis[i]=true;
		queue<int> q;
		q.push(i);
		cn=0;
		while(!q.empty()) {
			int u=q.front();
			q.pop();
			cc[cn++]=u;
			for(int i=0;i<con[u].size();i++) {
				int v=con[u][i];
				if(!vis[v]) {
					vis[v]=true;
					q.push(v);
				}
			}
		}
		if(cn==n) return -1;
		for(int i=0;i<cn;i++) {
			int u=cc[i];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int i=0;i<cn;i++) {
			int u=cc[i], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		un(i,idx);
		con[i].push_back(idx);
		con[idx].push_back(i);
		for(int i=0;i<cn;i++) {
			int u=cc[i];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
	void x() {
		for(int i=0;i<2;i++) {
			if(nxt[i]!=NULL) delete nxt[i];
		}
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.empty() || r.empty()) {
		solve(l,b-1); solve(r,b-1);
		return;
	}
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	root->x();
	delete root;
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	int msz=0;
	for(int i=1;i<=n;i++) {
		if(con[i].size()>msz) msz=con[i].size();
	}
	lli ret=0;
	bool fst=true;
	for(int i=1;i<con.size();i++) if(!con[i].empty() && !mark[i]) {
		if(fst && con[i].size()==msz) {
			fst=false; continue;
		}
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
	void x() {
		for(int i=0;i<2;i++) {
			if(nxt[i]!=NULL) {
				nxt[i]->x();
				delete nxt[i];
			}
		}
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.size()>r.size()) swap(l,r);
	if(l.empty() || r.empty()) {
		solve(l,b-1); solve(r,b-1);
		return;
	}
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	root->x();
	delete root;
	v=vector<int>();
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
	void x() {
		for(int i=0;i<2;i++) {
			if(nxt[i]!=NULL) delete nxt[i];
		}
		delete *this;
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1 || b<0) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.empty() || r.empty()) {
		solve(l,b-1); solve(r,b-1);
		return;
	}
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	root->x();
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

vector<int> con[200001];
bool vis[200001];

lli bv() {
	memset(vis,0,sizeof(vis));
	lli ret=0;
	for(int i=1;i<=n;i++) if(!vis[fd(i)]) {
		vis[i]=true;
		queue<int> q;
		q.push(i);
		vector<int> cc;
		while(!q.empty()) {
			int u=q.front();
			q.pop();
			cc.push_back(u);
			for(int i=0;i<con[u].size();i++) {
				int v=con[u][i];
				if(!vis[v]) {
					vis[v]=true;
					q.push(v);
				}
			}
		}
		if(cc.size()==n) return -1;
		for(int i=0;i<cc.size();i++) {
			int u=cc[i];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int i=0;i<cc.size();i++) {
			int u=cc[i], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		un(i,idx);
		con[i].push_back(idx);
		con[idx].push_back(i);
		for(int i=0;i<cc.size();i++) {
			int u=cc[i];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


#include <iostream>
#include <vector>

using namespace std;

typedef long long lli;

class p {
public:
	p* nxt[2];
	p() {
		nxt[0]=nxt[1]=NULL;
	}
	void ins(int x) {
		p* cur=this;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]==NULL) cur->nxt[to]=new p();
			cur=cur->nxt[to];
		}
	}
	int qry(int x) {
		p* cur=this;
		int ret=0;
		for(int i=29;i>=0;i--) {
			int to=0;
			if(x&(1<<i)) to=1;
			if(cur->nxt[to]!=NULL) cur=cur->nxt[to];
			else {
				cur=cur->nxt[to^1];
				ret|=(1<<i);
			}
		}
		return ret;
	}
};

int a[300001], n;
lli res;

void solve(vector<int>& v, int b) {
	if(v.size()<=1) return;
	vector<int> l, r;
	for(int i=0;i<v.size();i++) {
		if(v[i]&(1<<b)) r.push_back(v[i]);
		else l.push_back(v[i]);
	}
	if(l.empty() || r.empty()) return;
	p* root=new p();
	for(int i=0;i<l.size();i++)
		root->ins(l[i]);
	int add=2147483647;
	for(int i=0;i<r.size();i++) {
		int w=root->qry(r[i]);
		if(w<add) add=w;
	}
	res+=add;
	solve(l,b-1); solve(r,b-1);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	vector<int> v(n);
	for(int i=0;i<n;i++)
		cin>>v[i];
	solve(v,29);
	cout<<res<<"\n";
	return 0;
}


#pragma GCC optimize("O3")

#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
#include <set>

using namespace std;

typedef long long lli;

int L[6300000], R[6300000], cnt[6300000], m=1;
vector<int> vx;
set<int> s[200000];

void ins(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			if(!R[u]) R[u]=m++;
			cnt[R[u]]++;
			u=R[u];
		} else {
			if(!L[u]) L[u]=m++;
			cnt[L[u]]++;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].insert(k);
}

void del(int u, int x, int k) {
	for(int i=29;i>=0;i--) {
		if(x&(1<<i)) {
			cnt[R[u]]--;
			u=R[u];
		} else {
			cnt[L[u]]--;
			u=L[u];
		}
	}
	s[lower_bound(vx.begin(),vx.end(),x)-vx.begin()].erase(k);
}

int qry(int u, int x) {
	int ret=0;
	for(int i=29;i>=0;i--) {
		int to=L[u], miss=R[u];
		if(x&(1<<i)) swap(to,miss);
		if(!to || !cnt[to]) {
			ret|=(1<<i);
			u=miss;
		} else u=to;
	}
	return ret;
}

int a[200001], n, up[200001];

int fd(int u) {
	if(!up[u]) return u;
	return up[u]=fd(up[u]);
}

void un(int u, int v) {
	int ru=fd(u), rv=fd(v);
	up[rv]=ru;
}

bool mark[200001];

lli bv() {
	memset(mark,0,sizeof(mark));
	vector<vector<int> > con(n+1);
	for(int i=1;i<=n;i++)
		con[fd(i)].push_back(i);
	if(con[fd(1)].size()==n) return -1;
	lli ret=0;
	for(int i=1;i<con.size();i++) if(!con[i].empty() && !mark[i]) {
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			del(1,a[u],u);
		}
		int mn=2147483647, idx, val;
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j], w=qry(1,a[u]);
			if(w<mn) {
				mn=w; val=w^a[u];
			}
		}
		idx=*s[lower_bound(vx.begin(),vx.end(),val)-vx.begin()].begin();
		ret+=mn;
		mark[fd(idx)]=true;
		un(con[i][0],idx);
		for(int j=0;j<con[i].size();j++) {
			int u=con[i][j];
			ins(1,a[u],u);
		}
	}
	return ret;
}

int main() {
	scanf("%d",&n);
	int root=m++;
	for(int i=1;i<=n;i++) {
		scanf("%d",&a[i]);
		vx.push_back(a[i]);
	}
	sort(vx.begin(),vx.end());
	vx.erase(unique(vx.begin(),vx.end()),vx.end());
	for(int i=1;i<=n;i++)
		ins(root,a[i],i);
	lli res=0;
	while(1) {
		lli add=bv();
		if(add<0) break;
		res+=add;
	}
	printf("%lld\n",res);
	return 0;
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=8e6+9;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}

inline void read(int &x){
  char c=nc(),b=1;
  if(c==EOF){
      x=-1;
      return ;
  }
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL ans=0;
void dfs(int rt,int dep){
    if(dep==0)return ;
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        ans+=res;
    }
    if(ls)dfs(ls,dep-1);
    if(rs)dfs(rs,dep-1);
}

int main(){
    int n;read(n);
    for(int i=1;i<=n;i++)read(a[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    dfs(0,30);
    printf("%lld\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0) return 0;
    if(l[rt]==r[rt])return (LL)a[l[rt]];
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return fin(val,son[rt][!to],dep-1);
}

LL dfs(int rt,int dep){
    if(son[rt][0]&&son[rt][1]){
        LL res=2e18;
        for(int i=l[son[rt][0]];i<=r[son[rt][0]];i++){
            res=min(res,a[i]^fin(a[i],son[rt][1],dep-1));
        }
        return res+dfs(son[rt][0],dep-1)+dfs(son[rt][1],dep-1);
    }
    else if(son[rt][0])return dfs(son[rt][0],dep-1);
    else if(son[rt][1])return dfs(son[rt][1],dep-1);
    return 0;
}

int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

int read(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL dfs(int rt,int dep){
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        return res+dfs(ls,dep-1)+dfs(rs,dep-1);
    }
    if(ls)return dfs(ls,dep-1);
    if(rs)return dfs(rs,dep-1);
    return 0;
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

int fin(int val,int rt,int dep){
    if(dep==0) return 0;
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return (1<<dep-1)+fin(val,son[rt][!to],dep-1);
}

int dfs(int rt,int dep){
    if(son[rt][0]&&son[rt][1]){
        int res=2e9;
        for(int i=l[son[rt][0]];i<=r[son[rt][0]];i++){
            res=min(res,(1<<dep-1)+fin(a[i],son[rt][1],dep-1));
        }
        return res+dfs(son[rt][0],dep-1)+dfs(son[rt][1],dep-1);
    }
    else if(son[rt][0])return dfs(son[rt][0],dep-1);
    else if(son[rt][1])return dfs(son[rt][1],dep-1);
    return 0;
}

int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);
    for(int i=1;i<=num;i++){
        //printf("%d %d\n",l[i],r[i]);
    }
    int ans=dfs(0,30);
    printf("%d\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0) return 0;
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return (1ll<<dep-1)+fin(val,son[rt][!to],dep-1);
}

LL dfs(int rt,int dep){
    if(son[rt][0]&&son[rt][1]){
        LL res=2e18;
        for(int i=l[son[rt][0]];i<=r[son[rt][0]];i++){
            res=min(res,(1ll<<dep-1)+fin(a[i],son[rt][1],dep-1));
        }
        return res+dfs(son[rt][0],dep-1)+dfs(son[rt][1],dep-1);
    }
    else if(son[rt][0])return dfs(son[rt][0],dep-1);
    else if(son[rt][1])return dfs(son[rt][1],dep-1);
    return 0;
}

int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=2000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}

inline void read(int &x){
  char c=nc(),b=1;
  if(c==EOF){
      x=-1;
      return ;
  }
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL ans=0;
void dfs(int rt,int dep){
    if(dep==0)return ;
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        ans+=res;
    }
    if(ls)dfs(ls,dep-1);
    if(rs)dfs(rs,dep-1);
}

int main(){
    int n;read(n);
    for(int i=1;i<=n;i++)read(a[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    dfs(0,30);
    printf("%lld\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

int read(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return fin(val,son[rt][!to],dep-1);
}

LL dfs(int rt,int dep){
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        return res+dfs(ls,dep-1)+dfs(rs,dep-1);
    }
    if(ls)return dfs(ls,dep-1);
    if(rs)return dfs(rs,dep-1);
    return 0;
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

int read(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return fin(val,son[rt][!to],dep-1);
}

LL dfs(int rt,int dep){
    if(son[rt][0]&&son[rt][1]){
        LL res=2e18;
        for(int i=l[son[rt][0]];i<=r[son[rt][0]];i++){
            res=min(res,a[i]^fin(a[i],son[rt][1],dep-1));
        }
        return res+dfs(son[rt][0],dep-1)+dfs(son[rt][1],dep-1);
    }
    if(son[rt][0])return dfs(son[rt][0],dep-1);
    if(son[rt][1])return dfs(son[rt][1],dep-1);
    return 0;
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

int read(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL ans=0;
void dfs(int rt,int dep){
    if(dep==0)return ;
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        ans+=res;
    }
    if(ls)dfs(ls,dep-1);
    if(rs)dfs(rs,dep-1);
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    dfs(0,30);
    printf("%lld\n",ans);
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

int read(){
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL dfs(int rt,int dep){
    if(dep==0)return 0;
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        return res+dfs(ls,dep-1)+dfs(rs,dep-1);
    }
    if(ls)return dfs(ls,dep-1);
    if(rs)return dfs(rs,dep-1);
    return 0;
}

int main(){
    int n=read();
    for(int i=1;i<=n;i++)a[i]=read();
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%lld\n",ans);
}


/*

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
  return *p1++;
}

inline void read(int &x){
  char c=nc(),b=1;
  if(c==EOF){
      x=-1;
      return ;
  }
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=(a[pos]>>i)&1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0||l[rt]==r[rt]) return (LL)a[l[rt]];
    int to=1&(val>>dep-1);
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else if(son[rt][!to]) return fin(val,son[rt][!to],dep-1);
    else return 0;
}

LL ans=0;
void dfs(int rt,int dep){
    if(dep==0)return ;
    int ls=son[rt][0],rs=son[rt][1];
    if(ls&&rs){
        LL res=2e18;
        for(int i=l[ls];i<=r[ls];i++){
            res=min(res,a[i]^fin(a[i],rs,dep-1));
        }
        ans+=res;
    }
    if(ls)dfs(ls,dep-1);
    if(rs)dfs(rs,dep-1);
}

int main(){
    int n;read(n);
    for(int i=1;i<=n;i++)read(a[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    dfs(0,30);
    printf("%lld\n",ans);
}


/*#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define fir first
#define sec second
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define maxd 1000000007
#define eps 1e-6
typedef long long ll;
const int N=100000;
const double pi=acos(-1.0);
struct node{
	int ch[2];
}trie[6006000];
int n,a[600600],l[6006000],r[6006000],tot=0;
ll ans=0;

int read()
{
	int x=0,f=1;char ch=getchar();
	while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
	while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
	return x*f;
}

void insert(int x,int id)
{
	int now=0;
	per(i,30,0)
	{
		int tmp=(x>>i)&1;
		if (!trie[now].ch[tmp])
		{
			trie[now].ch[tmp]=(++tot);
			l[tot]=id;r[tot]=id;
		}
		now=trie[now].ch[tmp];
		l[now]=min(l[now],id);r[now]=max(r[now],id);
	}
}

int query(int id,int x,int d)
{
	if ((d<0) || (l[id]==r[id])) return a[l[id]];
	int tmp=(x>>d)&1;
	if (trie[id].ch[tmp]) return query(trie[id].ch[tmp],x,d-1);
	else if (trie[id].ch[tmp^1]) return query(trie[id].ch[tmp^1],x,d-1);
	else return 0;
}

void dfs(int id,int d)
{
	if (d<0) return;
	if ((trie[id].ch[0]) && (trie[id].ch[1]))
	{
		int res=maxd,rson=trie[id].ch[1],lson=trie[id].ch[0];
		rep(i,l[lson],r[lson])
			res=min(res,a[i]^query(rson,a[i],d-1));
		ans+=res;
	}
	if (trie[id].ch[0]) dfs(trie[id].ch[0],d-1);
	if (trie[id].ch[1]) dfs(trie[id].ch[1],d-1);
}

int main()
{
	n=read();
	rep(i,1,n) a[i]=read();
	sort(a+1,a+1+n);
	rep(i,1,n) insert(a[i],i);
	//rep(i,1,tot) cout << l[i] << " " << r[i] << endl;
	dfs(0,30);
	printf("%lld",ans);
	return 0;
}
*/
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1000009;
int a[maxn],l[maxn],r[maxn],son[maxn][2],num;

void insert(int pos){
    int rt=0;
    for(int i=29;i>=0;i--){
        int to=0;
        if(a[pos]&(1<<i))to=1;
        if(!son[rt][to])son[rt][to]=++num,l[num]=r[num]=pos;
        rt=son[rt][to];
        l[rt]=min(l[rt],pos),
        r[rt]=max(r[rt],pos);
    }
}

LL fin(int val,int rt,int dep){
    if(dep==0) return 0;
    int to=0;
    if(val&(1<<dep-1))to=1;
    if(son[rt][to])return fin(val,son[rt][to],dep-1);
    else return (1ll<<dep-1)+fin(val,son[rt][!to],dep-1);
}

LL dfs(int rt,int dep){
    if(son[rt][0]&&son[rt][1]){
        LL res=2e18;
        for(int i=l[son[rt][0]];i<=r[son[rt][0]];i++){
            res=min(res,(1ll<<dep-1)+fin(a[i],son[rt][1],dep-1));
        }
        return res+dfs(son[rt][0],dep-1)+dfs(son[rt][1],dep-1);
    }
    else if(son[rt][0])return dfs(son[rt][0],dep-1);
    else if(son[rt][1])return dfs(son[rt][1],dep-1);
    return 0;
}

int main(){
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)scanf("%d",a+i);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)insert(i);

    LL ans=dfs(0,30);
    printf("%d\n",ans);
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define lli long long int
#define fop(i,m,n) for (int i=(m);i<(n);i++)
#define FOP(i,m,n) for (int i=(m)-1;i>=(n);--i)
#define test(x) cout << #x << ' ' << x << endl;
#define printv(a) {\
    for (auto i : a) cout << i << ' ';\
    cout << '\n';\
}
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<lli,lli>
#define tiii tuple<int,int,int>
#define all(a) a.begin(), a.end()
#define X first
#define Y second
#define oset tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>
auto SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(SEED);
const int mod = 998244353, x = 864197532, N = 8000, logN = 17;

struct trie {
	trie* ch[2];
	trie() {
		ch[0] = ch[1] = NULL;
	}
	void insert (int v, int d = 29) {
		if (d < 0) return;
		bool f = v & (1 << d);
		if (!ch[f]) ch[f] = new trie();
		ch[f]->insert(v, d - 1);
	}
	int query (int v, int d = 29) {
		if (d < 0) return 0;
		bool f = v & (1 << d);
		if (ch[f]) return ch[f]->query(v, d - 1);
		return ch[!f]->query(v, d - 1) ^ (1 << d);
	}
};

lli get_ans(vector <int> &a, int l, int r, int bit) {
	if (l == r || bit < 0) return 0;
	int m;
	for (m = l; m < r && !(a[m] & (1 << bit)); ++m);
	lli ans = get_ans(a, l, m, bit - 1) + get_ans(a, m, r, bit - 1);
	if (m != l && m != r) {
		trie* s = new trie();
		fop (i,l,m) s->insert(a[i]);
		int tmp = 1 << 30;
		fop (i,m,r) {
			tmp = min(tmp, s->query(a[i]));
		}
		ans += tmp;
	}
	return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector <int> a(n);
    fop (i,0,n) cin >> a[i];
    sort(all(a));
    cout << get_ans(a, 0, n, 29) << endl;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
#define lli long long int
#define fop(i,m,n) for (int i=(m);i<(n);i++)
#define FOP(i,m,n) for (int i=(m)-1;i>=(n);--i)
#define test(x) cout << #x << ' ' << x << endl;
#define printv(a) {\
    for (auto i : a) cout << i << ' ';\
    cout << '\n';\
}
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<lli,lli>
#define tiii tuple<int,int,int>
#define all(a) a.begin(), a.end()
#define X first
#define Y second
#define oset tree<pii,null_type,less<pii>,rb_tree_tag,tree_order_statistics_node_update>
auto SEED = chrono::steady_clock::now().time_since_epoch().count();
mt19937 rng(SEED);
const int mod = 998244353, x = 864197532, N = 8000, logN = 17;

struct trie {
	trie* ch[2];
	int sz;
	trie() {
		ch[0] = ch[1] = NULL;
		sz = 0;
	}
	void insert (int v, int d = 29) {
		sz++;
		if (d < 0) return;
		bool f = v & (1 << d);
		if (!ch[f]) ch[f] = new trie();
		ch[f]->insert(v, d - 1);
	}
	void erase (int v, int d = 29) {
		sz--;
		if (d < 0) return;
		bool f = v & (1 << d);
		ch[f]->erase(v, d - 1);
		if (sz == 0) delete this;
	}
	int query (int v, int d = 29) {
		if (d < 0) return 0;
		bool f = v & (1 << d);
		if (ch[f] && sz > 0) return ch[f]->query(v, d - 1);
		return ch[!f]->query(v, d - 1) ^ (1 << d);
	}
	void release(int d = 29) {
		if (ch[0]) ch[0]->release(d - 1);
		if (ch[1]) ch[1]->release(d - 1);
		if (d < 29) delete this;
	}
};

lli get_ans(vector <int> &a, int l, int r, int bit) {
	if (l == r || bit < 0) return 0;
	int m;
	for (m = l; m < r && !(a[m] & (1 << bit)); ++m);
	lli ans = get_ans(a, l, m, bit - 1) + get_ans(a, m, r, bit - 1);
	trie* s = new trie();
	if (m != l && m != r) {
		fop (i,l,m) s->insert(a[i]);
		int tmp = 1 << 30;
		fop (i,m,r) {
			tmp = min(tmp, s->query(a[i]));
		}
		fop (i,l,m) s->erase(a[i]);
		ans += tmp;
	}
	return ans;
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector <int> a(n);
    fop (i,0,n) cin >> a[i];
    sort(all(a));
    cout << get_ans(a, 0, n, 29) << endl;
}