Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

signed main() {
	int n, m; cin >> n >> m;
	vector<bool> bs(n);
	rep(i, n) {
		int x; cin >> x;
		bs[i] = x;
	}
	vector<bool> a(n), b(n);
	int tb = 0;
	int idx = 0;
	rep(i, m) {
		int p; cin >> p;
		rep(j, p) {
			a[idx] = tb;
			b[idx] = !tb;
			idx++;
		}
		tb = !tb;
	}
	int mb = 1 << 15;
	
	using P = pair<vector<bool>, int>;
	unordered_map<vector<bool>, int> visited;
	queue<P> que;
	que.push(P(bs, 0));
	visited[bs] = 0;
	int ans = -1;
	
	while (!que.empty()) {
		P p = que.front(); que.pop();
		auto& pb = p.first;
		auto& pc = p.second;
		if (pb == a || pb == b) {
			ans = pc;
			break;
		}
		rep(i, n - 1) {
			vector<bool> next = pb;
			swap(next[i], next[i + 1]);
			int nc = pc + 1;
			if (visited.count(next)) continue;
			visited[next] = nc;
			que.emplace(next, nc);
		}
	}
	cout << ans << endl;
	
	
}