Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<iostream>
#include<algorithm>
#include<set>
#include<cstdio>
#include<cstdlib>
#include<cmath>
//#include<queue>
using namespace std;
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define FORD(i,j,k) for(int i=j;i>=k;i--)
#define LL long long
#define SZ(x) x.size()
int vis[40000];
int x,k,n,m,s,ans,start,end1,end2,dight;

int bfs()
{
	int q1[40000];int L1=1;int R1=0;	
	int q2[40000];int L2=1;int R2=0;
	q1[++R1]=start;
	q2[++R2]=1;
	vis[start]=1;
	while(q1[L1]!=end1&&q1[L1]!=end2)
	{
		int fr=q1[L1];
		int fv=q2[L2];
		FOR(i,0,n-2)
		{
			if((fr&(1<<i))&&!(fr&(1<<(i+1))))
			{
				ans=fr+(1<<i);
				if(!vis[ans]) {vis[ans]=1;q1[++R1]=ans;q2[++R2]=fv+1;}
			}
			else if(!(fr&(1<<i))&&(fr&(1<<(i+1))))
			{
				ans=fr-(1<<i);
				if(!vis[ans]) {vis[ans]=1;q1[++R1]=ans;q2[++R2]=fv+1;}
			}
		}
		L1++;
		L2++;
	}
	return q2[L2]-1;
}
int main()
{
cin>>n>>m;
dight=1;
FORD(i,n-1,0)
{
	cin>>x;
	start+=x<<i;
}
int c=n;
FOR(i,1,m)
{
	cin>>x;
	FOR(j,1,x)
	{
		c--;
		end1+=dight<<c;
		end2+=(1-dight)<<c;
	}
	dight^=1;
}
cout<<bfs()<<endl;
return 0;
}