Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include<bits/stdc++.h>
#include<map>
#include<queue>
#include<cmath>
#include<cassert>
#include<vector>
#include<iostream>
using namespace std;

vector<int> bits;
vector<int> after;

void print(vector<int> v){
  for(auto i:v){
    //cerr<<i<<" ";
  }//cerr<<endl;
}

void init(){
  bits.clear();
  after.clear();
}

bool input(){
  int n,m;
  cin>>n>>m;
  for(int i=0;i<n;i++){
    int tmp;
    cin>>tmp;
    bits.push_back(tmp);
  }
  for(int i=0;i<m;i++){
    int tmp;
    cin>>tmp;
    after.push_back(tmp);
  }
  return true;
}


int make_ans(vector<int> pre,vector<int> a,vector<int> b){
  queue<pair<int,vector<int>>> q;
  map<vector<int>,int> dp;
  q.push(make_pair(0,pre));
  while(!q.empty()){
    pair<int,vector<int>> now = q.front();q.pop();
    const vector<int> v = now.second;
    const int cost = now.first;
    if(v==a)return cost;
    if(v==b)return cost;
    if(dp.count(v)!=0)continue;
    dp[v]=cost;
    for(int i=0;i+1<v.size();i++){
      vector<int> next= v;
      swap(next[i],next[i+1]);
      q.push(make_pair(cost+1,next));
    }
  }
  return INT_MAX;
}

int solve(){
  vector<int> b;
  bool jj = false;
  for(int i=0;i<after.size();i++){
    for(int j=0;j<after[i];j++){
      b.push_back(jj);
    }
    jj = (!jj);
  }
  print(bits);
  print(b);
  vector<int> a = b;
  for(int i=0;i<b.size();i++){
    a[i] = (!a[i]);
  }
  print(b);
  int ans = make_ans(bits,a,b);
  return ans;
}

int main(){
  init();input();
  cout<<solve()<<endl;
}