Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <bitset>

using namespace std;

struct state{
	vector< int > bits;
	int depth;
};

vector< int > count_bits( vector< int > *b ) {
	vector< int > v;
	int cnt = 0;
	for( int i = 0; i < b ->size(); i++ ) {
		cnt ++;
		if( i < b -> size() - 1 && b -> at( i ) != b -> at( i + 1 ) ) {
			v.push_back( cnt );
			cnt = 0;
		}
	}
	v.push_back( cnt );
	return v;
}

bool isEqual( vector< int > *v, vector< int > *w ) {
	if( v->size() != w->size() ) return false;
	for( int i = 0; i < v->size(); i++ ) {
		if( v->at(i) != w->at(i) ) return false;
	}
	return true;
}

void swap_bits( vector< int > *b, int pos ) {
	int c = b -> at( pos );
	b -> at( pos ) = b -> at( pos + 1 );
	b -> at( pos + 1 ) = c;
	return;
}

int main(void){

	int N, M;
	cin >> N >> M;
	state start;
	start.bits = vector< int >( N, 0 );
	start.depth = 0;
	vector< int > p( M, 0 );
	vector< bool > visited( 1 << N, false );

	for( int i = 0; i < N; i++ )
		cin >> start.bits[i];
	for( int i = 0; i < M; i++ )
		cin >> p[i];

	queue< state > q;
	q.push( start );

	while( !q.empty() ) {
		
		state cs = q.front();
		q.pop();
		vector< int > v = count_bits( &cs.bits );
		if( isEqual( &v, &p ) ) { 
			cout << cs.depth << endl;
			break;
		}
		
		for( int i = 0; i < N - 1; i++ ) {
			state newstate;
			for( int j = 0; j < N; j++ )
				newstate.bits.push_back( cs.bits[j] );

			int n = 0;
			swap_bits( &newstate.bits, i );

			for( int j = 0; j < N; j++ ) {
				if( newstate.bits[j] ) n |= ( 1 << j );
			}

			if( visited[ n ] ) continue;
			visited[n] = true;

			newstate.depth = cs.depth + 1;
			
			q.push( newstate );
		}


	}


    return 0;
}