#include<bits/stdc++.h>
#define ll long long
#define Add_Speed ios_base::sync_with_stdio(0);cin.tie(NULL);
#define pb push_back
#define mp make_pair
#define f first
#define sz size()
#define int ll
#define s second
#define pii pair<int, int>
#define all(s) s.begin(), s.end()
#define file(s) freopen(s".in","r",stdin); freopen(s".out","w",stdout);
using namespace std;
const int maxn = (int)1e5 + 100;
const int M = (int)2e6 + 100;
const int mod = (int)1e9 + 7;
const int inf = mod;
int n, a[maxn], s, cnt[60][2];
main () {
cin >> n >> s;
for(int i = 1; i <= n; i++){
cin >> a[i];
for(int j = 0; j < 60; j++){
int bit = (a[i] >> j) & 1;
cnt[j][bit]++;
}
}
map<int, int> was;
for(int i = 0; i < (1 << 20); i++){
int res = 0, val = 0;
for(int j = 0; j < 20; j++){
int bit = ((i >> j) & 1);
val += bit * (1ll << j);
res += cnt[j][bit ^ 1] * (1ll << j);
}
was[res] = val;
}
for(int i = 0; i < (1 << 20); i++){
int res = 0, val = 0;
for(int j = 0; j < 20; j++){
int bit = ((i >> j) & 1);
val += bit * (1ll << (j + 20));
res += cnt[j + 20][bit ^ 1] * (1ll << (j + 20));
}
if(was[s - res]){
cout << was[s - res] + val;
return 0;
}
}
cout << -1;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KIAojZGVmaW5lIGxsIGxvbmcgbG9uZwojZGVmaW5lIEFkZF9TcGVlZCBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApO2Npbi50aWUoTlVMTCk7CiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgZiBmaXJzdAojZGVmaW5lIHN6IHNpemUoKQojZGVmaW5lIGludCBsbAojZGVmaW5lIHMgc2Vjb25kICAgICAgICAgIAojZGVmaW5lIHBpaSBwYWlyPGludCwgaW50PgojZGVmaW5lIGFsbChzKSBzLmJlZ2luKCksIHMuZW5kKCkKI2RlZmluZSBmaWxlKHMpIGZyZW9wZW4ocyIuaW4iLCJyIixzdGRpbik7IGZyZW9wZW4ocyIub3V0IiwidyIsc3Rkb3V0KTsKCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKY29uc3QgaW50IG1heG4gPSAoaW50KTFlNSArIDEwMDsKY29uc3QgaW50IE0gPSAoaW50KTJlNiArIDEwMDsKY29uc3QgaW50IG1vZCA9IChpbnQpMWU5ICsgNzsKY29uc3QgaW50IGluZiA9IG1vZDsgICAgICAgICAgICAgICAgCgppbnQgbiwgYVttYXhuXSwgcywgY250WzYwXVsyXTsgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAptYWluICgpIHsKICBjaW4gPj4gbiA+PiBzOwogIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKXsKICAgIGNpbiA+PiBhW2ldOwogICAgZm9yKGludCBqID0gMDsgaiA8IDYwOyBqKyspewogICAgICBpbnQgYml0ID0gKGFbaV0gPj4gaikgJiAxOwogICAgICBjbnRbal1bYml0XSsrOwogICAgfQogIH0KICBtYXA8aW50LCBpbnQ+IHdhczsKICBmb3IoaW50IGkgPSAwOyBpIDwgKDEgPDwgMjApOyBpKyspewogICAgaW50IHJlcyA9IDAsIHZhbCA9IDA7CiAgICBmb3IoaW50IGogPSAwOyBqIDwgMjA7IGorKyl7CiAgICAgIGludCBiaXQgPSAoKGkgPj4gaikgJiAxKTsKICAgICAgdmFsICs9IGJpdCAqICgxbGwgPDwgaik7ICAKICAgICAgcmVzICs9IGNudFtqXVtiaXQgXiAxXSAqICgxbGwgPDwgaik7CiAgICB9CiAgICB3YXNbcmVzXSA9IHZhbDsKICB9ICAgICAgICAgICAgICAgICAgICAgICAgCiAgZm9yKGludCBpID0gMDsgaSA8ICgxIDw8IDIwKTsgaSsrKXsKICAgIGludCByZXMgPSAwLCB2YWwgPSAwOwogICAgZm9yKGludCBqID0gMDsgaiA8IDIwOyBqKyspewogICAgICBpbnQgYml0ID0gKChpID4+IGopICYgMSk7CiAgICAgIHZhbCArPSBiaXQgKiAoMWxsIDw8IChqICsgMjApKTsgIAogICAgICByZXMgKz0gY250W2ogKyAyMF1bYml0IF4gMV0gKiAoMWxsIDw8IChqICsgMjApKTsKICAgIH0gICAgICAgICAgICAKICAgIGlmKHdhc1tzIC0gcmVzXSl7CiAgICAgIGNvdXQgPDwgd2FzW3MgLSByZXNdICsgdmFsOwogICAgICByZXR1cm4gMDsKICAgIH0KICB9ICAgICAgICAgICAgICAgICAgICAgICAgCiAgY291dCA8PCAtMTsKCgp9