Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <algorithm>

using namespace std;
typedef pair<string, int> P;

void solve()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	bool used[10000];
	memset(used, 0, sizeof(used));
	int N, M;
	cin >> N >> M;
	string goal;
	for (int i = 0; i < N; ++i)
	{
		char c;
		cin >> c;
		goal += c;
	}
	string p;
	string start[2];
	bool flag = false;
	for (int i = 0; i < M; ++i)
	{
		int c;
		cin >> c;
		for (int j = 0; j < c; ++j)
		{
			if (flag)
			{
				start[0] += '0';
				start[1] += '1';
			}
			else
			{
				start[0] += '1';
				start[1] += '0';
			}
		}
		flag = !flag;
	}
	if (start[0] == goal || start[1] == goal)
	{
		cout << 0 << endl;
		return;
	}

	queue<P> que;
	que.push(P(start[0], 0));
	que.push(P(start[1], 0));
	set<string> found;
	found.insert(start[0]);
	found.insert(start[1]);
	while (!que.empty())
	{
		P digit = que.front();
		que.pop();
		for (int i = 1; i < digit.first.size(); ++i)
		{
			swap(digit.first[i - 1], digit.first[i]);
			if (found.find(digit.first) != found.end())
			{
				swap(digit.first[i - 1], digit.first[i]);
				continue;
			}
			if (digit.first == goal)
			{
				cout << digit.second + 1 << endl;
				return;
			}
			que.push(P(digit.first, digit.second + 1));
			found.insert(digit.first);
			swap(digit.first[i - 1], digit.first[i]);
		}
	}
}

int main()
{
	solve();
	return(0);
}