#include <bits/stdc++.h>

using namespace std;

int a[1000005];

int main()
{
    //freopen("input.txt","r",stdin);
    int n,k;
    cin >> n >> k;
    int Max = 0;
    for (int i=0;i<n;++i){
        cin >> a[i];
        Max = max(Max,a[i]);
    }
    if (n<=20){
        int m = n;
        int ans = 0;
        for (int msk=0;msk<(1<<m);++msk){
            if (msk&1) continue;
            vector <int> v;
            v.push_back(0);
            for (int bit=0;bit<m;++bit){
                if (msk&(1<<bit)) v.push_back(bit);
            }
            v.push_back(n);
            if (v.size()!=k+1) continue;
            int loc = -1;
            for (int i=0;i+1<v.size();++i){
                int val = 0;
                for (int j=v[i];j<v[i+1];++j) val |= a[j];
                if (loc==-1) loc = val;
                else loc &= val;
            }
            ans = max(ans,loc);
        }
        cout << ans;
        return 0;
    }
    if (Max==1){
        int fr1 = 0;
        for (int i=0;i<n;++i) fr1 += a[i];
        if (fr1>=k) cout << 1;
        else cout << 0;
        return 0;
    }
    if (Max==2){
        int fr1 = 0,fr2  = 0;
        for (int i=0;i<n;++i){
            if (a[i]==1) fr1++;
            if (a[i]==2) fr2++;
        }
        if (fr1<k and fr2<k) cout << 0;
        else if (fr1>=k and fr2<k) cout << 1;
        else if (fr1<k and fr2>=k) cout << 2;
        else{
            bool is1 = 0,is2 = 0;
            int ind = 0;
            int nr = 0;
            while (ind<n){
                if (a[ind]==1) is1 = 1;
                if (a[ind]==2) is2 = 1;
                if (is1 and is2){
                    nr++;
                    is1 = 0;
                    is2 = 0;
                }
                ind++;
            }
            if (nr>=k) cout << 3;
            else cout << 2;
        }
        return 0;
    }
    assert(0);
    return 0;
}
