Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define endl '\n'
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int,int> PI;

int main(int argc, char *argv[])
{
  int n,m;
  cin >> n >> m;
  string a;
  rep(i,n){
    char ch;
    cin >> ch;
    a += ch;
  }

  string g1,g2;
  rep(j,m){
    int t;
    cin >> t;
    rep(jj,t){
      g1 += char('0'+j%2);
      g2 += char('1'-j%2);
    }
  }
  
  queue<pair<int,string> > q;
  set<string> app;
  q.push(mp(0,a));
  while(!q.empty()){
    auto cv=q.front().S;
    int cc =q.front().F;
    q.pop();
    if(app.count(cv)) continue;
    app.insert(cv);
    if(cv == g1 || cv == g2){
      cout << cc << endl;
      return 0;
    }
    
    rep(i,n-1){
      swap(cv[i],cv[i+1]);
      if(!app.count(cv))
        q.push(mp(cc+1,cv));
      swap(cv[i],cv[i+1]);      
    }
  }
  
  return 0;
}