Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <map>
#include <cstring>
#include <cassert>
#include <vector>
#include <queue>
using namespace std;

template<class T>
ostream& operator<<(ostream& os, const vector<T>& vs) {
    if (vs.empty()) return os << "[]";
    os << "[" << vs[0];
    for (int i = 1; i < vs.size(); i++) os << " " << vs[i];
    return os << "]";
}

int N, M;
vector<int> S;
vector<int> T;
void input() {
    cin >> N >> M;
    S.clear(); S.resize(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i];
    }
    T.clear(); T.resize(M);
    for (int i = 0; i < M; i++) {
        cin >> T[i];
    }
}

vector<int> A, B;
void init() {
    A.clear(); B.clear();
    int x = 0;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < T[i]; j++) {
            A.push_back(x);
        }
        x = !x;
    }
    x = 1;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < T[i]; j++) {
            B.push_back(x);
        }
        x = !x;
    }
}

const int MAX_N = 15;

struct State {
    vector<int> bit;
    int t;
    State(vector<int> bit, int t) : bit(bit), t(t) {}
};

void solve() {
    init();
    map< vector<int>, bool > used;
    queue<State> Q;
    used[S] = true;
    Q.push(State(S, 0));
    while (!Q.empty()) {
        State cur = Q.front(); Q.pop();
        if (cur.bit == A || cur.bit == B) {
            cout << cur.t << endl;
            break;
        }
        for (int i = 0; i < N - 1; i++) {
            vector<int> nbit = cur.bit;
            if (nbit[i] == nbit[i + 1]) continue;
            swap(nbit[i], nbit[i + 1]);
            if (used[nbit]) continue;
            used[nbit] = true;
            Q.push(State(nbit, cur.t + 1));
        }
    }
}

int main() {
    input(); solve();
    return 0;
}