#include <iostream>
#pragma GCC optimize("O3,unroll-loops")
#include <algorithm>
#include <cstring>
#include <climits>
#include <iomanip>
#include <numeric>
#include <bitset>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#define int long long
//#define int short
#define pb push_back
#define f first
#define s second
using namespace std;

const int nmax = 1e6;
const int valmax = 1e9;
const int bitmax = 30;

int n, k, v[nmax + 5], calc[25][25], x[25], rez = 0;
vector<int> p1, p2;

void bk(int poz, int val){
    if (val < rez){
        return;
    }
    for (int i = x[poz - 1] + 1; i <= n - (k - poz); i++){
        x[poz] = i;
        int cnt = val & calc[x[poz - 1] + 1][i];
        if (poz == k){
            rez = max(rez, cnt);
        }
        else{
            bk(poz + 1, cnt);
        }
    }
}

signed main(){
    cin>>n>>k;
    for (int i = 1; i <= n; i++){
        cin>>v[i];
        if (v[i] == 1){
            p1.pb(i);
        }
        if (v[i] == 2){
            p2.pb(i);
        }
    }
    if (n <= 20){
        for (int i = 1; i <= n; i++){
            int f = 0;
            for (int j = i; j <= n; j++){
                f |= v[j];
                calc[i][j] = f;
            }
        }
        bk(1, (1 << bitmax) - 1);
        cout<<rez;
        return 0;
    }
    if (p1.size() >= k && p2.size() < k){
        rez = 1;
    }
    if (p1.size() < k && p2.size() >= k){
        rez = 2;
    }
    if (p1.size() >= k && p2.size() >= k){
        int l1 = 0, l2 = 0, cnt = 0, poz = 0;
        while (cnt < k){
            cnt++;
            poz = max(p1[l1++], p2[l2++]);
            while (l1 < p1.size() && p1[l1] <= poz){
                l1++;
            }
            while (l2 < p2.size() && p2[l2] <= poz){
                l2++;
            }
            if (l1 == p1.size() || l2 == p2.size()){
                break;
            }
        }
        if (cnt == k){
            rez = 3;
        }
    }
    cout<<rez;
}

/*

vector<int> solve(vector<int> v){
    map<int, int> M;
    sort(v.begin(), v.end());
    int n = v.size();
    for (auto i : v){
        M[i]++;
    }
    bool t1 = true;
    for (auto i : M){
        if (i.s % 2 == 1){
            t1 = false;
        }
    }
    bool t2 = true;
    for (auto i : M){
        for (int j = 0; j <= 30; j++){
            if (i.f == (1 << j)){
                break;
            }
        }
        t2 = false;
    }
    bool t3 = true;;
    for (int i = 1; i < n; i++){
        if (v[i] != v[i - 1] + 1){
            t3 = true;
        }
    }
    vector<int> rez;
    if (t1){
        for (int idk = 1; idk <= 2; idk++){
            for (auto i : M){
                for (int j = 1; j <= i.s / 2; j++){
                    rez.pb(i.f);
                }
            }
        }
    }
    if (t2){
        for (int i = 0; i < n / 2; i++){
            rez.pb(v[i]);
        }
        rez.pb(v[n - 1]);
        for (int i = n / 2; i < n - 1; i++){
            rezz.pb(v[i]);
        }
    }
    if (t3){

    }
    return rez;
}

*/
