Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
using P = pair<int,int>;
int main(){
    int dist[1<<15];
    fill(dist,dist+(1<<15),1000000000);
    int N,M;
    cin>>N>>M;
    int start=0,goalA=0,goalB=0;
    for(int i=0;i<N;i++){
        int b;
        cin>>b;
        start<<=1;
        start+=b;
    }
    for(int i=0;i<M;i++){
        int p;
        cin>>p;
        for(int j=0;j<p;j++){
            goalA<<=1;
            goalA+=int(i%2==0);
            goalB<<=1;
            goalB+=int(i%2==1);
        }
    }
    queue<P> Q;
    Q.push(P(start,0));
    dist[start]=0;
    while(!Q.empty()){
        int cur, cost;
        tie(cur,cost)=Q.front(); Q.pop();
        if(cur==goalA||cur==goalB){
            cout<<cost<<endl;
            return 0;
        }
        if(dist[cur]<cost) continue;
        for(int i=0;i<N-1;i++){
            if(((cur>>i)&1)==((cur>>(i+1))&1)) continue;
            int next = cur^(3<<i);
            if(dist[next]<=cost+1) continue;
            dist[next]=cost+1;
            Q.push(P(next,cost+1));
        }
    }
    return 0;
}