You are given a complete undirected graph with *n* vertices. A number *a*<sub class="lower-index">*i*</sub> is assigned to each vertex, and the weight of an edge between vertices *i* and *j* is equal to *a*<sub class="lower-index">*i*</sub> *xor* *a*<sub class="lower-index">*j*</sub>.

Calculate the weight of the minimum spanning tree in this graph.
The first line contains *n* (1 ≤ *n* ≤ 200000) — the number of vertices in the graph.

The second line contains *n* integers *a*<sub class="lower-index">1</sub>, *a*<sub class="lower-index">2</sub>, ..., *a*<sub class="lower-index">*n*</sub> (0 ≤ *a*<sub class="lower-index">*i*</sub> &lt; 2<sup class="upper-index">30</sup>) — the numbers assigned to the vertices.
none
5
1 2 3 4 5
8
4
1 2 3 4
8
1
1
0
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds; 

#define ff              first
#define ss              second
#define ll              int64_t
#define ld              long double
#define nl              cout<<"\n"
#define i128            __int128_t
#define all(v)          v.begin(),v.end()
#define mset(a,v)       memset((a),(v),sizeof(a))
#define forn(i,a,b)     for(int64_t i=int64_t(a);i<int64_t(b);++i)
#define forb(i,a,b)     for(int64_t i=int64_t(a);i>=int64_t(b);--i)
#define fastio()        ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define mod         1'000'000'007
#define mod2        998'244'353 
#define inf         1'000'000'000'000'007
#define pi          3.14159265358979323846

template<class key,class cmp=std::less<key>>
using ordered_set=tree<key,null_type,cmp,rb_tree_tag,tree_order_statistics_node_update>;

template<class L,class R> ostream& operator<<(ostream& out,pair<L,R> &p)        {return out<<"("<<p.ff<<", "<<p.ss<<")";}
template<class T> ostream& operator<<(ostream& out,vector<T> &v)                {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,deque<T> &v)                 {out<<"[";for(auto it=v.begin();it!=v.end();++it){if(it!=v.begin())out<<", ";out<<*it;}return out<<"]";}
template<class T> ostream& operator<<(ostream& out,set<T> &s)                   {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class T> ostream& operator<<(ostream& out,ordered_set<T> &s)           {out<<"{";for(auto it=s.begin();it!=s.end();++it){if(it!=s.begin())out<<", ";out<<*it;}return out<<"}";}
template<class L,class R> ostream& operator<<(ostream& out,map<L,R> &m)         {out<<"{";for(auto it=m.begin();it!=m.end();++it){if(it!=m.begin())out<<", ";out<<*it;}return out<<"}";}

void dbg_out() {cerr<<"]\n";}
template<typename Head,typename... Tail> 
void dbg_out(Head H,Tail... T) {cerr<<H;if(sizeof...(Tail))cerr<<", ";dbg_out(T...);}
#ifdef LOCAL
#define dbg(...) cerr<<"["<<#__VA_ARGS__<<"] = [",dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

//---------------------------------mars4---------------------------------

class Trie
{
	struct Node
	{
		ll children[2];
		ll cnt;
		ll ed;

		Node()
		{
			children[0]=-1;
			children[1]=-1;
			cnt=0;
			ed=0;
		}
	};

	public:
	vector<Node> trie;

	void init(ll reserve_size=0)
	{
		if(reserve_size)
		{
			trie.reserve(reserve_size);
		}
		trie.push_back(Node());
	}

	void insert(ll val)
	{
		ll ind=0;
		forb(i,29,0)
		{
			bool bit=val&(1ll<<i);
			if(trie[ind].children[bit]==-1)
			{
				trie.push_back(Node());
				trie[ind].children[bit]=(ll)trie.size()-1;
			}
			trie[ind].cnt++;
			ind=trie[ind].children[bit];
		}
		trie[ind].cnt++;
		trie[ind].ed++;
	}

	ll min_xor(ll val)
	{
		ll res=0;
		ll ind=0;
		forb(i,29,0)
		{
			bool bit=val&(1ll<<i);
			if(trie[ind].children[bit]!=-1)
			{
				ind=trie[ind].children[bit];
			}
			else
			{
				res^=1ll<<i;
				ind=trie[ind].children[!bit];
			}
		}
		return res;
	}
};

vector<ll> a;
ll ans=0;

void calc(vector<ll> v,ll bit)
{
	if(v.empty() or bit<0)
	{
		return;
	}
	vector<ll> l,r;
	Trie T;
	T.init();
	for(ll i:v)
	{
		if(i&(1ll<<bit))
		{
			r.push_back(i);
			T.insert(i);
		}
		else
		{
			l.push_back(i);
		}
	}
	calc(l,bit-1);
	calc(r,bit-1);
	if(!r.empty() and !l.empty())
	{
		ll val=inf;
		for(ll i:l)
		{
			val=min(val,T.min_xor(i));
		}
		ans+=val;
	}
}

int main()
{
	fastio();
	ll z,n,m,t,k,i,j,l,d,h,r;
	cin>>n;
	a=vector<ll>(n);
	for(ll &i:a)
	{
		cin>>i;
	}
	sort(all(a));
	a.resize(unique(all(a))-a.begin());
	calc(a,29);
	cout<<ans<<"\n";
	cerr<<"\nTime elapsed: "<<1000*clock()/CLOCKS_PER_SEC<<"ms\n";
	return 0;
}
