Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <set>
#include <map>

using namespace std;

#define FORU(i, l, r) for (int i = l; i <= r; ++i)
#define FORD(i, r, l) for (int i = r; i >= l; --i)
#define REPU(i, r) for (int i = 0; i < r; ++i)
#define LL long long
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define PINT pair < ll, int >
#define BIT(x, i) ((x >> i) & 1)
#define NUM_SET_BIT __builtin_popcountll

int n, k, a[20], d[20], q[1 << 20], check[1 << 20];

bool ok(int u) {
    int c = 1, j = 1;
    FORD(i, n - 2, 0) {
        if (BIT(u, i) != BIT(u, i + 1)) {
            if (j > k) return false;
            if (c != d[j]) return false;
            c = 0; ++j;
        }
        ++c;
    }
    if (j != k) return false;
    if (c != d[j]) return false;
    return true;
}

int main() {
    cin >> n >> k;
    FORU(i, 1, n) scanf("%d", &a[i]);
    FORU(i, 1, k) scanf("%d", &d[i]);
    reverse(a + 1, a + n + 1);
    FORU(i, 1, n) q[1] += (a[i] << (i - 1));
    
    // BFS
    int l = 1, r = 1;
    check[q[1]] = 1;
    while (l <= r) {
        int u = q[l++];
        if (ok(u)) { cout << check[u] - 1 << endl; return 0; }
        FORU(i, 0, n - 2) {
            auto j = i + 1;
            auto bit_i = BIT(u, i);
            auto bit_j = BIT(u, j);
            auto v = u - 
                    (bit_i << i) - 
                    (bit_j << j) +
                    (bit_i << j) +
                    (bit_j << i);
            if (check[v]) continue;
            check[v] = check[u] + 1;
            q[++r] = v;
        }
    }
}