#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#define NMAX 1000000
//#define int unsigned int

using namespace std;

int backtrack(int n, int k, int last, int s, int mand);

bitset<NMAX+2> x;
int v[NMAX+2],sp[NMAX+2];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    bool c2 = true, c4 = true;
    int n,k,maxi = -1;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>v[i];
        maxi = max(maxi,v[i]);
        sp[i] = sp[i-1] | v[i];
        if(v[i]!=1 && v[i]!=0)
            c2 = false;
        if(v[i]!=0 && v[i]!=1 && v[i]!=2)
            c4 = false;
    }
    if(c2 == true)
    {
        int cnt=0;
        for(int i=1;i<=n;i++)
            if(v[i]==1)
                cnt++;
        if(cnt>=k)
            cout<<1;
        else
            cout<<0;
    }
    else if(c4==true)
    {
        int cnt1=0,cnt2=0;
        for(int i=1;i<=n;i++)
        {
            if(v[i]==1)
                cnt1++;
            if(v[i]==2)
                cnt2++;
        }
        if(cnt1<k && cnt2<k)
            cout<<0;
        else if(cnt1>=k && cnt2 < k)
            cout<<1;
        else if(cnt1<k && cnt2>=k)
            cout<<2;
        else
        {
            int apar1 = 0, apar2 = 0,o=0;
            for(int i=1;i<=n;i++)
            {
                if(v[i]==1)
                    apar1 = 1;
                if(v[i]==2)
                    apar2 = 1;
                if(apar1==apar2 && apar1==1)
                {
                    apar1 = apar2 = 0;
                    o++;
                }
            }
            if(o>=k)
                cout<<3;
            else
                cout<<2;
        }
    }
    else
    {
        int ans = 0;
        //cout<<maxi<<" ";
        for(int j=maxi;j>=0;j--)
        {
            int st = 0;
            int o=0;
            for(int i=1;i<=n;i++)
            {
                if((st&j)==j)
                {
                    o++;
                    st = 0;
                }
                st = st | v[i];
            }
            if((st&j)==j)
            {
                o++;
                st = 0;
            }
            if(o>=k)
            {
                ans = j;
                break;
            }
        }
        cout<<ans;
    }
    return 0;
}

int backtrack(int n, int k, int last, int s, int mand)
{
    if(s==k)
    {
        int nr = 0;
        for(int i=last;i<=n;i++)
            nr = nr | v[i];
        return (nr & mand);
    }
    int depus = k-s;
    int ans = -1;
    int st = v[last];
    for(int i = last+1;i<=n-depus+1;i++)
    {
        ans = max(ans,backtrack(n,k,i,s+1,st & mand));
        st = st | v[i];
    }
    return ans;
}
