Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <vector>
#include <algorithm>

#define REP(i,a,b) for(int i=int(a);i<int(b);i++)

using namespace std;

typedef long long int lli;

int bitSwap(int num, int p) {
    int t = (num >> p) & 1;
    num -= (t << p);
    int e = (num >> (p + 1)) & 1;
    num -= (e << (p + 1));
    return num + (t << (p + 1)) + (e << p);
}

int main () {
    int n, m;
    cin >> n >> m;
    int num = 0;
    REP (i, 0, n) {
        int bit;
        cin >> bit;
        num = (num << 1) + bit;
    }
    int s0 = 0, s1 = 0;
    REP (i, 0, m) {
        int p;
        cin >> p;
        REP (j, 0, p) {
            s0 = (s0 << 1) + (i % 2);
            s1 = (s1 << 1) + !(i % 2);
        }
    }
    int size = 1 << n;
    const int inf = 1 << 30;
    vector<int> dp(size, inf);
    dp[s0] = dp[s1] = 0;
    bool run = true;
    while (run) {
        run = false;
        REP (i, 0, size) {
            if (dp[i] == inf) continue;
            REP (j, 0, n - 1) {
                int t = bitSwap(i, j);
                if (dp[t] > dp[i] + 1) {
                    dp[t] = dp[i] + 1;
                    run = true;
                }
            }
        }
    }
    cout << dp[num] << endl;
    return 0;
}