#include <bits/stdc++.h>

#define pb push_back
#define sz(x) (ll)x.size()
#define pw(x) ((1LL)<<(x))
#define all(x) x.begin(), x.end()

using ll = int;
using ld = long double;

using namespace std;

const ll N = 1e6 + 52;

ll n, m;
ll i, j;
ll a[N];
vector<ll> p[30];
ll ans[30][N];
string s;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    for (i=0;i<n;i++){
        cin >> a[i];
    }

    for (j=0;j<30;j++){
        ll pos = -1;
        for (i=n-1;i>=0;i--){
            if ((a[i]>>j)&1){
                p[j].pb(i);
                pos = i;
            }
            ans[j][i] = pos;
        }
    }

    vector<ll> used;
    ll answer=0;

    vector<ll> mx;

    for (i=29;i>=0;i--){
//        cout << sz(p[i]) << endl;
        if (sz(p[i])>=m){
            reverse(all(p[i]));
            ll pos=-1;
            ll flag=0;
            ll kol=0;
            vector<ll> mx2;
            ll fl = 0;
            ll w=0;
//            cout << "i=" << i << endl;
            for (auto x : p[i]){
                if (kol>=m) break;
                if (x<=pos) continue;
                if (flag) break;
                if (w<sz(mx) && x<=mx[w] && !fl){
                    pos = mx[w];
                    w++;
                    kol++;
                    mx2.pb(pos);
                    continue;
                }else fl = 1;
//                cout << pos << " " << kol << endl;
                ll mx=x;
                for (auto x : used){
                    if (ans[x][pos+1]==-1){
                        flag = 1;
                        break;
                    }
                    mx=max(mx,ans[x][pos+1]);
                }
                pos = mx;
                mx2.pb(pos);
                kol++;
            }
            if (kol<m) flag = 1;
            if (!flag){
                mx = mx2;
                used.pb(i);
                answer+=pw(i);
            }
        }
    }
    cout << answer << endl;
    return 0;
}
