Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

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

    int n, m;
    cin >> n >> m;
    int st = 0;
    for (int i = 0; i < n; i++)
    {
        st *= 2;
        int b;
        cin >> b;
        st += b;
    }
    int gt1 = 0, gt2 = 0;
    for (int i = 0; i < m; i++)
    {
        int p;
        cin >> p;
        gt1 = gt1 << p;
        gt2 = gt2 << p;
        if (i % 2)
        {
            gt2 += (1 << p) - 1;
        }
        else
        {
            gt1 += (1 << p) - 1;
        }
    }
    vector<int> d(1 << 20, INF);
    vector<int> tmp(n);
    queue<int> q;
    d[st] = 0;
    q.push(st);
    while (!q.empty())
    {
        int v = q.front();
        q.pop();
        for (int i = 0; i < n; i++)
        {
            tmp[n - 1 - i] = (v >> i) & 1;
        }
        for(int i=0;i<n-1;i++){
            if(tmp[i] == tmp[i+1]) continue;
            swap(tmp[i], tmp[i+1]);
            int next=0;
            for(int j=0;j<n;j++){
                next *= 2;
                next += tmp[j];
            }
            swap(tmp[i], tmp[i+1]);
            if(d[next]!=INF) continue;
            d[next] = d[v] + 1;
            q.push(next);
        }
    }
    cout << min(d[gt1], d[gt2]) << endl;
}

