#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll a,b;
    cin>>a>>b;
    vector <ll> c,d;
    for (ll i=0;i<a;i++)
    {
        ll m;
        cin>>m;
        c.push_back(m);
        d.push_back(m);
    }
    vector <ll> mangan(b);
    if (a==b)
    {
        ll g=pow(2,63)-1;
        for (int i=0;i<a;i++)
        g=g&c[i];
        cout<<g;
        return 0;
    }
    else
    {
        ll result=b;
        sort (d.rbegin(),d.rend());
        vector <ll> ORS;
        ll OR=0;
        ll AND=0;
        ll l=0;
        while (d.size()>b)
            d.pop_back();
        for (ll i=0;i<a;i++)
        {
            for (ll j=0;j<b;j++)
            {
                if (c[i]==d[j])
                {
                    for (ll k=l;k<=i;k++)
                    {
                        OR=OR|c[k];
                    }
                    ORS.push_back(OR);
                    OR=0;
                    int temporary=d[j];
                    d[j]=d[b];
                    d.pop_back();
                    l=i;
                    break;
                }
            }
        }
        int finalresult=pow(2,63)-1;
        for (int i=0;i<result;i++)
        {
            finalresult=finalresult&ORS[i];
        }
        cout<<finalresult;
    }
    return 0;
}
