#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];
string s;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    vector<vector<ll>> ans(30, vector<ll> (n+1,-1));
    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;

    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;
//            cout << "i=" << i << endl;
            for (auto x : p[i]){
//                cout << pos << " " << kol << endl;
                if (kol>=m) break;
                if (x<=pos) continue;
                if (flag) break;
                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;
                kol++;
            }
            if (kol<m) flag = 1;
            if (!flag){
                used.pb(i);
                answer+=pw(i);
            }
        }
    }
    cout << answer << endl;
    return 0;
}
