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
typedef pair<int, int> P;
const int INF = 1e9;

int N, M;
vector<int> p;

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  cin >> N >> M;

  int bit = 0;
  for ( int i = 0; i < N; i++ ) {
    int b;
    cin >> b;
    bit |= (b<<i);    
  }

  p = vector<int>(M);
  for ( int i = 0; i < M; i++ ) {
    cin >> p[i];    
  }

  vector<bool> used(1<<N, false);
  priority_queue<P, vector<P>, greater<P> > Q;
  Q.push({0, bit});

  while ( !Q.empty() ) {
    P e = Q.top(); Q.pop();
    int c = e.first, b = e.second;    

    vector<int> q;
    int cnt = 1;
    bool pre = b&1;  
    for ( int i = 1; i < N; i++ ) {
      if ( pre != (bool)(b&(1<<i)) ) {
	q.emplace_back(cnt);
	pre = !pre;
	cnt = 1;      
      } else {
	cnt++;      
      }
    }
    q.emplace_back(cnt);

    if ( q == p ) {
      cout << c << endl;
      return 0;
    }

    if ( used[b] ) continue;
    used[b] = true;

    for ( int i = 0; i < N-1; i++ ) {
      int nxt = b;
      nxt &= ~(1<<i);
      nxt &= ~(1<<(i+1));
      if ( b & (1<<i) ) nxt |= (1<<(i+1));
      if ( b & (1<<(i+1)) ) nxt |= (1<<i);
      Q.push(P(c+1, nxt));      
    }
  }
  
  return 0;
}

