Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

int bfs(V &start, V &run) {
    int N = start.size();
    int M = run.size();

    map<V, int> d;
	queue<V> que;
    que.push(start);
    d[start] = 0;

    V g1;
    int bit = 0;
    for (auto x : run) {
        rep(k, x) g1.emplace_back(bit);
        bit ^= 1;
    }

    V g2 = g1;
    rep(i, N) g2[i] ^= 1;

    while (!que.empty()) {
        auto now = que.front(); que.pop();
        if (now == g1 || now == g2) {
            return d[now];
        }

        rep(i, N - 1) {
            V nxt(now);
            swap(nxt[i], nxt[i + 1]);
            if (d.count(nxt) == 0) {
                que.push(nxt);
                d[nxt] = d[now] + 1;
            }
        }

    }

}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N, M;
    cin >> N >> M;
    V start(N);
    rep(i, N) cin >> start[i];
    V run(M);
    rep(i, M) cin >> run[i];

    cout << bfs(start, run) << endl;

}