#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,k,a[1048576],dp[2048][2048],cnt[5];
vector <int> v[5];
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1; i<=n; i++)
    {
        cin>>a[i];
        cnt[a[i]]++;
        v[a[i]].push_back(i);
    }
    int p=0;
    int i1=0,i2=0;
    int br=0;
    while(i1<v[1].size()&&i2<v[2].size())
    {
        while(i1<v[1].size()&&v[1][i1]<p)i1++;
        if(i1==v[1].size())break;
        while(i2<v[2].size()&&v[2][i2]<p)i2++;
        if(i2==v[2].size())break;
        br++;
        p=max(v[1][i1],v[2][i2]);
    }
    if(br>=k)
    {
        cout<<3<<endl;
        return 0;
    }
    p=0;
    i1=0,i2=0;
    br=0;
    while(i1<v[1].size()&&i2<v[2].size())
    {
        while(i2<v[2].size()&&v[2][i2]<p)i2++;
        if(i2==v[2].size())break;
        while(i1<v[1].size()&&v[1][i1]<p)i1++;
        if(i1==v[1].size())break;
        p=max(v[1][i1],v[2][i2]);
        br++;
    }
    if(br>=k)
    {
        cout<<3<<endl;
        return 0;
    }
    if(v[2].size()>=k)cout<<2<<endl;
    else if(v[1].size()>=k)cout<<1<<endl;
    else cout<<0<<endl;
    return 0;
}
