#include <iostream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
//#define int long long
//#define int short
#define pb push_back
#define f first
#define s second
using namespace std;

const int nmax = 1e6;
const int valmax = 1e9;
const int bitmax = 30;

int n, k, v[nmax + 5], rez = 0, l[bitmax + 5], tc = 0;
bool t23 = true;
vector<int> p[bitmax], bt;

bool verif(){
    int cnt = 0;
    while (cnt < k){
        cnt++;
        int mx = 0, mn = nmax;
        for (auto i : bt){
            tc++;
            mx = max(mx, p[i][l[i]++]);
        }
        bool ok = true;
        for (auto i : bt){
            tc += 1 + log2(p[i].size());
            l[i] = upper_bound(p[i].begin(), p[i].end(), mx) - p[i].begin();
            int len = p[i].size() - l[i];
            mn = min(mn, len);
            if (l[i] == p[i].size() || mn < k - cnt - 1){
                ok = false;
                break;
            }
        }
        if (!ok){
            break;
        }
    }
    if (cnt >= k){
        return true;
    }
    return false;
}

signed main(){
    cin>>n>>k;
    for (int i = 1; i <= n; i++){
        cin>>v[i];
        if (v[i] > 2){
            t23 = false;
        }
        for (int j = 0; j < bitmax; j++){
            tc++;
            if (v[i] & (1 << j)){
                p[j].pb(i);
            }
        }
    }
    int mb = -1;
    for (int i = 0; i < bitmax; i++){
        tc++;
        if (p[i].size() >= k){
            mb = i;
        }
    }
    if (mb == -1){
        cout<<0;
        return 0;
    }
    if (!t23 && n == 1e6){
        cout<<-1;
    }
    bt.pb(mb);
    for (int bit = mb - 1; bit >= 0; bit--){
        if (p[bit].size() < k){
            continue;
        }
        bt.pb(bit);
        for (int i = bit + 1; i <= mb; i++){
            tc++;
            l[i] = 0;
        }
        if (verif()){
            rez += (1 << bit);
        }
        else{
            bt.pop_back();
        }
    }
    cout<<rez + (1 << mb);
}


/*

vector<int> solve(vector<int> v){
    map<int, int> M;
    sort(v.begin(), v.end());
    int n = v.size();
    for (auto i : v){
        M[i]++;
    }
    bool t1 = true;
    for (auto i : M){
        if (i.s % 2 == 1){
            t1 = false;
        }
    }
    bool t2 = true;
    for (auto i : M){
        for (int j = 0; j <= 30; j++){
            if (i.f == (1 << j)){
                break;
            }
        }
        t2 = false;
    }
    bool t3 = true;;
    for (int i = 1; i < n; i++){
        if (v[i] != v[i - 1] + 1){
            t3 = true;
        }
    }
    vector<int> rez;
    if (t1){
        for (int idk = 1; idk <= 2; idk++){
            for (auto i : M){
                for (int j = 1; j <= i.s / 2; j++){
                    rez.pb(i.f);
                }
            }
        }
    }
    if (t2){
        for (int i = 0; i < n / 2; i++){
            rez.pb(v[i]);
        }
        rez.pb(v[n - 1]);
        for (int i = n / 2; i < n - 1; i++){
            rezz.pb(v[i]);
        }
    }
    if (t3){

    }
    return rez;
}

*/
