#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ff first
#define ss second
#define pii pair<int,int>
#define vii vector <int>
#define endl '\n'
const int inf = 1e18;
const int N = 2e5+5;
inline void test_case() {
    int n, k;
    cin >> n >> k;
    int a[n];
    int cnt1 = 0, cnt2 = 0;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] == 1) cnt1++;
        if (a[i] == 2) cnt2++;
    }
    if (cnt2 < k) {
        if (cnt1 >= k) {
            cout << 1 << endl;
        } else cout << 0 << endl;
    } else {
        if (cnt1 < k) {
            cout << 2 << endl;
        } else {
            bool ok1 = false, ok2 = false;
            int p = 0;
            for (int i = 0; i < n; i++) {
                if (a[i] == 1) ok1 = 1;
                if (a[i] == 2) ok2 = 1;
                if (ok1 && ok2) {
                    p++;
                    ok1 = ok2 = 0;
                }
            }
            if (p >= k) {
                cout << 3 << endl;
            } else cout << 2 << endl;
        }
    }
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int T = 1;
    // cin >> T;
    while (T--) test_case();
}