#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#define NMAX 1000000

using namespace std;

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

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

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    bool first = true,c2 = true, c4 = true;
    int n,k,operatii = 1,y;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>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;
        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
        cout<<backtrack(n,k,1,1);
    return 0;
}

int backtrack(int n, int k, int last, int s)
{
    if(s==k)
    {
        bool first = true;
        int t=0;
        int st = 0;
        for(int i=1;i<=n;i++)
        {
            if(x[i]==1)
            {
                if(first==true)
                {
                    t = st;
                    first = false;
                }
                else
                    t = t & st;
                st = v[i];
            }
            st = st | v[i];
        }
        t = t & st;
        return t;
    }
    int depus = k-s;
    int ans = -1;
    for(int i = last+1;i<=n-depus+1;i++)
    {
        x[i] = 1;
        ans = max(ans,backtrack(n,k,i,s+1));
        x[i] = 0;
    }
    return ans;
}
