#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e6;
int a[MAXN + 1] , sep[MAXN + 1];
int n , k , maxa , i;
void bkt ( int poz , int nrs ) {
    if ( poz == n ) {
        if ( nrs == k - 1 ) {
            int val , ans;
            val = 0;
            ans = ( 1 << 30 ) - 1;
            for ( i = 0 ; i < n ; i++ ) {
                if ( !sep[i] )
                    val |= a[i];
                else {
                    ans &= val;
                    val = a[i];
                }
            }
            maxa = max ( maxa , ans & val );
        }
    } else {
        sep[poz] = 0;
        bkt ( poz + 1 , nrs );
        sep[poz] = 1;
        bkt ( poz + 1 , nrs + 1 );
    }
}
int main () {
    ios_base :: sync_with_stdio ( 0 );
    cin.tie ( 0 );

    cin >> n >> k;
    for ( i = 0 ; i < n ; i++ )
        cin >> a[i];
    if ( n <= 20 ) {
        bkt ( 1 , 0 );
        cout << maxa << '\n';
        return 0;
    } else {
        int g1 = 1 , g2 = 1 , g3 = 1 , ap1 , ap2;
        for ( i = 0 ; i < n ; i++ )
            if ( a[i] == 1 )
                g1++;
        for ( i = 0 ; i < n ; i++ )
            if ( a[i] == 2 )
                g2++;
        ap1 = ap2 = 0;
        for ( i = 0 ; i < n ; i++ ) {
            if ( a[i] == 1 )
                ap1 = 1;
            else
                ap2 = 1;
            if ( ap1 && ap2 )
                g3++;
        }
        if ( g3 >= k )
            cout << 3 << '\n';
        else if ( g2 >= k )
            cout << 2 << '\n';
        else if ( g1 >= k )
            cout << 1 << '\n';
        else
            cout << 0 << '\n';
    }
    return 0;
}
