#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    ll A, B;
    cin >> A >> B;
    ll niza[A];
    for(ll i=0; i<A; i++){
        cin >> niza[i];
    }

                    ll one, two=0;
                    for(int i=0; i<A; i++){
                        if(niza[i]==2) two++;
                        if(niza[i]==1) one++;
                    }
                    if(two>=B){
                        bool o[B]={false}, t[B]={false};
                        ll counter=0;
                        ll i=0;
                        while(i<A || counter <B){
                            if(niza[i]==1){
                                o[counter]=true;
                            }
                            if(niza[i]==2){
                                t[counter]=true;
                            }
                            if(o[counter]==true && t[counter]==true) counter++;
                        }
                        if(counter=B){
                            cout << 3;
                        }
                        else{
                            cout << 2;
                        }

                    }
                    else if(one>=B){
                        cout << 1;
                    }
                    else{
                        cout << 0;
                    }
}
