Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <map>
#include <queue>
#include <string>
#include <vector>
#include <utility>
#include <iostream>
#include <algorithm>

#define INF (1 << 29)

using namespace std;

int BFS(string S, string T) // S --> T
{
	map<string, int> D; D[S] = 1;

	queue<pair<string, int> > que; que.push(make_pair(S, 1));

	if (S == T) { return 0; }

	while (!que.empty())
	{
		string S2 = que.front().first;

		int dist = que.front().second; que.pop();

		for (int i = 0; i < T.size() - 1; i++)
		{
			string S3 = S2;

			swap(S3[i], S3[i + 1]);

			if (S3 == T) { return dist; }

			if (D[S3] == 0)
			{
				D[S3] = dist + 1;

				que.push(make_pair(S3, dist + 1));
			}
		}
	}

	return INF;
}

int main()
{
	int N, M;

	cin >> N >> M;

	vector<int> B(N); for (int i = 0; i < N; i++) { cin >> B[i]; }
	vector<int> R(M); for (int i = 0; i < M; i++) { cin >> R[i]; }

	string S, T1, T2;

	for (int i = 0; i < N; i++) { S += B[i] + 48; }

	for (int i = 0; i < M; i++)
	{
		for (int j = 0; j < R[i]; j++) 
		{
			T1 += i % 2 + 48;
			T2 += (i + 1) % 2 + 48;
		}
	}

	int res1 = BFS(S, T1);
	int res2 = BFS(S, T2);

	cout << min(res1, res2) << endl;

	return 0;
}