Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <queue>

using namespace std;

int main()
{
    int n, m;
    cin >> n >> m;
    int bits = 0;
    for (int i = 0; i < n; i++) {
        int input;
        cin >> input;
        bits <<= 1;
        bits |= input;
    }
    int goal[2] = {0, 0};
    int seq;
    for (int i = 0; i < m; i++) {
        cin >> seq;
        for (int j = 0; j < seq; j++) {
            goal[0] <<= 1;
            goal[0] |= i % 2;
        }
    }
    int range = (1 << n) - 1;
    goal[1] = ~goal[0] & range;
    int mask = 0b11;
    int dp[65536];
    fill_n((int *)dp, 65535, -2);
    dp[bits] = 0;
    dp[goal[0]] = dp[goal[1]] = -1;
    if (bits == goal[0] || bits == goal[1]) {
        cout << 0 << endl;
        exit(0);
    }
    queue<int> que;
    que.push(bits);
    while(!que.empty()){
        int p = que.front(); que.pop();
        for (int i = 0; i < n-1; i++) {
            if ((~p & (mask << i)) == 0 || (p & (mask << i)) == 0) continue;
            int swaped = (~p & (mask << i)) | (p & ~(mask << i));
            if (dp[swaped] == -1) {
                cout << dp[p] + 1 << endl;
                goto end;
            }
            if (dp[swaped] != -2) continue;
            que.push(swaped);
            dp[swaped] = dp[p] + 1;
        }
    }
    cout << 0 << endl;
    end:
    return 0;
}