#include<bits/stdc++.h> //NeOWami
using namespace std;
#define ft first
#define sc second
#define int long long
using pii = pair<int, int>;
using pipii = pair<int, pii>;
template <class T> using heapmin = priority_queue<T, vector<T>, greater<T>>;
const int N = 2e5 + 5;
int n, a[N];
int par[N];
int findPar(int u) {
if (u == par[u]) return u;
return par[u] = findPar(par[u]);
}
bool joint(int u, int v) {
u = findPar(u);
v = findPar(v);
if (u == v) return 0;
par[v] = u;
return 1;
}
namespace sub1 {
struct Edge{
int u, v, w;
};
vector<Edge> E;
void solve () {
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
E.push_back({i, j, a[i] ^ a[j]});
}
}
sort(E.begin(), E.end(), [&] (const Edge &x, const Edge &y) {
return x.w < y.w;
});
int ans = 0;
for (int i = 1; i <= n; i++) {
par[i] = i;
}
int cnt = 0;
for (Edge &e: E) {
int u = e.u, v = e.v, w = e.w;
if (joint(u, v)) ans += w, cnt++;
if (cnt == n - 1) break;
}
cout << ans;
}
};
namespace subfull {
const int LG = 29;
int ans = 0;
bool vis[N];
struct Node {
int child[2];
int cnt, exist;
};
vector<Node> Trie;
int newNode() {
int id = Trie.size();
Node tmp;
tmp.child[0] = tmp.child[1] = -1;
tmp.cnt = tmp.exist = 0;
Trie.push_back(tmp);
// cerr << id << "\n";
return id;
}
void addNum(int x, int t) {
int pos = 0;
for (int i = 29; i >= 0; i--) {
Trie[pos].cnt++;
int c = ((x >> i) & 1);
if (Trie[pos].child[c] == -1) {
int id = newNode();
Trie[pos].child[c] = id;
}
// cerr << x << " " << t << " "<< c << ": " << pos << " -> " << Trie[pos].child[c] << "\n";
pos = Trie[pos].child[c];
}
Trie[pos].cnt++;
Trie[pos].exist = t;
}
void delNum(int x) {
int pos = 0;
for (int i = 29; i >= 0; i--) {
// cerr << "DEL: " << pos << " " << x << " " << i << " " << Trie[pos].cnt << " -> ";
Trie[pos].cnt--;
// cerr << "CAI DEO J MA TU 2 XUONG CON ME SO 0 V: ";
// cerr << Trie[pos].cnt << "\n";
int c = ((x >> i) & 1);
pos = Trie[pos].child[c];
}
Trie[pos].cnt--;
Trie[pos].exist = 0;
// cerr << "DEL: "<< pos << " " << x << " " << Trie[pos].cnt << "\n";
}
int findXor(int x) {
int pos = 0;
// cerr << "CNT: " << Trie[pos].cnt << "\n";
for (int i = 29; i >= 0; i--) {
int c = (x >> i & 1);
// cerr << "finxor: " << x << " " << pos << " | " << i << " " << c << " -> ";
if (Trie[pos].child[c] == -1 || Trie[Trie[pos].child[c]].cnt == 0) c = (c ^ 1);
if (Trie[pos].child[c] == -1) return 0;
pos = Trie[pos].child[c];
// cerr << c << " " << pos << '\n';
}
return Trie[pos].exist;
}
void solve() {
if (n == 1) return cout << 0, void();
newNode();
for (int i = 2; i <= n; i++) addNum(a[i], i);
int tid = findXor(a[1]);
// cerr << tid << '\n';
int rem = 1;
vis[1] = 1;
heapmin<pipii> Q;
Q.push({a[1] ^ a[tid], {1, tid}});
// for (int i = 1; i <= n; i++) cerr << a[i] << " "; cerr << '\n';
while(rem < n && !Q.empty()) {
int old = Q.top().ft, rt = Q.top().sc.ft, nxt = Q.top().sc.sc;
Q.pop();
// cerr << old << " " << rt << " " << nxt << '\n';
if (vis[nxt]) {
int id = findXor(a[rt]);
if (id) Q.push({a[rt] ^ a[id], {rt, id}});
continue;
}
vis[nxt] = 1;
delNum(a[nxt]);
ans += old;
rem++;
int id = findXor(a[nxt]);
// cerr << nxt << " -> " << id << '\n';
if (id) Q.push({a[nxt] ^ a[id], {nxt, id}});
id = findXor(a[rt]);
// cerr << rt << " -> " << id << '\n';
if (id) Q.push({a[rt] ^ a[id], {rt, id}});
}
cout << ans;
}
};
signed main() {
cin.tie(NULL)->sync_with_stdio(false); cout.tie(NULL);
if (ifstream("XOR.INP")) {
freopen("XOR.INP", "r", stdin);
freopen("XOR.OUT", "w", stdout);
}
cin >> n;
vector<int> tmp(n); for (int i = 1; i <= n; i++) cin >> tmp[i - 1];
sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
n = tmp.size();
for (int i = 1; i <= n; i++) a[i] = tmp[i - 1];
// if (n <= 3000) return sub1::solve(), 0;
return subfull::solve(), 0;
return 0;
}