#include<bits/stdc++.h>
using namespace std;
int a[1000009];
int dp[2001][2001];
int f(int l, int r, int m){
    if (m == 1)return dp[l][r];
    int mx = 0;
    for (int i = l; i <= r - m; i ++){
        mx = max(mx, (f(i + 1, r, m - 1) & dp[l][i]));
    }
    return mx;
}
int main(){
    int n, m;
    cin >> n >> m;
    int mx = 0;
    for (int i = 1; i <= n; i ++){
        cin >> a[i];
        mx = max(mx, a[i]);
    }
    if (mx <= 2){
        int cnt1 = 0, cnt2 = 0;
        for (int i = 1; i <= n; i ++){
            if (a[i] == 1)cnt1 ++;
            else cnt2 ++;
        }
        if (cnt1 < m && cnt2 < m){cout << 0;return 0;}
        if (cnt1 < m){cout << 2;return 0;}
        if (cnt2 < m){cout << 1;return 0;}
        cnt1 = 0;
        bool ok1 = 0, ok2 = 0;
        for (int i = 1; i <= n; i ++){
            if (a[i] == 1)ok1 = 1;
            if (a[i] == 2) ok2 = 1;
            if (ok1 && ok2){
                cnt1 ++;
                ok1 = 0, ok2 = 0;
            }
        }
        if (cnt1 >= m)cout << 3;
        else cout << 2;
    }
    else {
        for (int i = 1; i <= n; i ++){
            dp[i][i] = a[i];
        }
        for (int len = 2; len <= n; len ++){
            for (int i = 1, j = i + len - 1; j <= n; i ++, j ++){
                dp[i][j] = (dp[i][j - 1] | dp[i + 1][j]);
            }
        }
        cout << f(1, n, m);
    }
}