Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<bits/stdc++.h>
using namespace std;

//幅優先探索

int main(){
    int n, m; cin >> n >> m;
    vector<bool> b(n);
    for(int i = 0; i < n; i++){
        int in; cin >> in;
        b[i] = (bool)in;
    }
    vector<int> p(m);
    for(int i = 0; i < m; i++) cin >> p[i];
    vector<bool> g1;
    vector<bool> g2;
    for(int i = 0; i < m; i++){
        if(i % 2 == 0){
            for(int j = 0; j < p[i]; j++){
                g1.push_back(false);
                g2.push_back(true);
            }
        }else{
            for(int j = 0; j < p[i]; j++){
                g1.push_back(true);
                g2.push_back(false);
            }
        }
    }

    set<vector<bool> > used;
    queue<pair<vector<bool>, int> > q;
    q.push({b, 0});
    used.insert(b);
    
    while(!q.empty()){

        vector<bool> vec = q.front().first;
        int cost = q.front().second;
        q.pop();
        
        if(vec == g1 || vec == g2){
            cout << cost << endl;
            break;
        }

        for(int i = 0; i < n - 1; i++){
            vector<bool> nxt = vec;
            swap(nxt[i], nxt[i + 1]);
            if(used.find(nxt) == used.end()){
                used.insert(nxt);
                q.push({nxt, cost + 1});
            }
        }
    }

    return 0;
}
