Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<bits/stdc++.h>  
using namespace std;
const int INF = 1 << 30;

int N, M, b, Exit, mask;
int v[1 << 15];
int ret;
void rec(int digit, int cnt){
  if(cnt >= ret) return;
  if((digit & mask) == (Exit & mask) || (digit & mask) == ((~Exit) & mask)){
    ret = min( ret, cnt);
    return;
  } else if(v[digit & mask] <= cnt){
    return;
  } else {
    v[digit] = cnt;
    const int prev = digit;
    for(int i = 0; i < N - 1; ++i){
      digit &= ~(3 << i);
      digit |= ((prev >> i) & 1) << (i + 1);
      digit |= ((prev >> (i + 1)) & 1) << i;
      rec( digit, cnt + 1);
      digit = prev;
    }
  }
}

int main(){
  fill_n( v, 1 << 15, INF);
  ret = INF;

  cin >> N >> M;
  mask = (1 << N) - 1;
  for(int i = 0; i < N; ++i){
    bool d;
    cin >> d;
    b |= d << (N - i - 1);
  }
  int cnt = 0;
  bool flip = false;
  for(int i = 0; i < M; ++i){
    int p;
    cin >> p;
    while(p--){
      Exit |= (int)flip << (N - cnt - 1);
      ++cnt;
    }
    flip ^= 1;
  }
  rec(b, 0);
  cout << ret << endl;
}