Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<cstring>
#include<string>
#include<map>
#include<cmath>
#include<vector>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 300000+10;
int n,m,a[20],b[20],st,ed;
int d[maxn];
int c[20],cnt,ans;

void dfs(int x,int now){
    if(x==n){
        cnt=0;
        memset(c,0,sizeof(c));
        for(int i=n-1;i>=0;i--){
            int x=((now&(1<<i))>>i),y=((now&(1<<(i+1)))>>(i+1));
            if(i==n-1||x==y) c[cnt]++;
            else  cnt++,c[cnt]=1;
        }
        if(cnt==m-1){
            bool flag=true;
            for(int i=0;i<=cnt;i++)
                if(c[i]!=b[i]) flag=false;
            if(flag) ans=min(ans,d[now|(1<<n)]);
        }
        return;
    }
    dfs(x+1,now);
    dfs(x+1,now|(1<<x));
}

void spfa(){
    queue<int> q;
    bool inq[maxn];
    for(int i=0;i<=ed;i++) d[i]=INF,inq[i]=0;
    d[st]=0,q.push(st);
    while(!q.empty()){
        int u=q.front(); q.pop(); inq[u]=0;
        for(int i=0;i<n-1;i++){
            int a=(((1<<i)&u)>>i),b=(((1<<(i+1))&u)>>(i+1));
            if(a!=b){
                int v=u^(1<<i)^(1<<(i+1));
                if(d[v]>d[u]+1){
                    d[v]=d[u]+1;
                    if(!inq[v]){
                        q.push(v);
                        inq[v]=1;
                    }
                }
            }
        }
    }
}

int main(){
    //freopen("a.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&a[n-1-i]);
    for(int i=0;i<m;i++) scanf("%d",&b[i]);
    a[n]=1;
    st=ed=0;
    //printf("%d\n",(1<<17));
    for(int i=0;i<=n;i++){
        if(a[i]) st|=(1<<i);
        ed|=(1<<i);
    }
    spfa();
    ans=INF;
    dfs(0,0);
    printf("%d\n",ans);
    return 0;
}