/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#define ll long long int
#define FOR(i, a, b) for(int i=(a);i<=(b);++i)
#define REP(i, n) for(int i=0;i<n;++i)
#define FOREACH(x, a) for(auto& (x) : (a))
#define ALL(a) (a).begin(),(a).end()
#define OUT(x) out << (x) << endl;
#define M0(x) memset(x,0,sizeof(x))
#define MINF(x) memset(x,0x3f,sizeof(x))
#define MMINUS(a) memset(a,0xff,sizeof(a))
#define ten(n) ((int)1e##n)

const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const ll LINF = 1e18;
const double EPS = 1.0/LINF;
const char ENDL = '\n'; // for JetBrains IDE only supports yen

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

using namespace std;

class DXXOR {
public:
    int N;
    ll K;
    ll A[ten(5)+1];
    int bit[ten(5)+1][41];
    int bit_K[41]{0};

    void solve(std::istream& in, std::ostream& out) {
        in >> N >> K;
        REP(i, N) in >> A[i];
        M0(bit);

        // O(1e6)
        ll b = 0;
        int keta = -1;
        while(b <= K) {
            keta++;
            b = 1ll << keta;
            REP(i, N) {
                bit[i][keta] = ((A[i] & b) > 0);
            }
        }

        ll max_x = 0;

        // keta_K * N = O(1e6)
        int keta_K = keta-1;
        FOR(k, 0, keta_K) {
            int cnt_z = 0;
            int cnt_o = 0;
            FOR(i, 0, N-1) {
                if (bit[i][k] == 0) cnt_z++;
                else if (bit[i][k] == 1) cnt_o++;
                else OUT("wwwwwww");
            }
            if (cnt_z > cnt_o) {
                bit_K[k] = 1;
                max_x |= (1ll << k);
            } else {
                bit_K[k] = 0;
            }
        }

        if (max_x <= K) {
            OUT(f(max_x));
            return;
        }

        // herasu
        stack<int> s;
        FOR(k, 0, keta_K) {
            if (bit_K[k] == 1) {
                max_x &= ~(1ll << k);
                s.push(k);
            }
            if (max_x <= K) {
                break;
            }
        }

        // last k is discarded
        s.pop();
        while(s.size()) {
            int num = s.top();
            ll tmp = max_x | (1ll << num);
            if (tmp <= K) {
                max_x = tmp;
            }
            s.pop();
        }

        OUT(f(max_x));
    }

    ll f(ll xi) {
        ll ans = 0;
        REP(i, N) {
            ans += A[i] ^ xi;
        }
        return ans;
    }
};

int main() {
    DXXOR solver;
    std::istream& in(std::cin);
    std::ostream& out(std::cout);
    solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 17152