Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, vector<int> > P;
const int INF = 1e9;

int N, M, a, b;
vector<int> v, d;
set< vector<int> > S;
queue<P> open;

bool solve(vector<int> &vec){
  vector<int> tmp;
  int cnt = 1;
  FOR(i, 1, vec.size()){
    if(vec[i] == vec[i - 1]) ++cnt;
    else{
      tmp.push_back(cnt);
      cnt = 1;
    }
  }
  tmp.push_back(cnt);
  if(M != (int)(tmp.size())) return false;
  REP(i, M) if(tmp[i] != d[i]) return false;
  return true;
}

int main() {
  cin >>N >>M;
  int n = 0;
  d = vector<int>(M);
  vector<int> v(N);
  REP(i, N) cin >>v[i];
  REP(i, M) cin >>d[i];
  open.push( P(0, v) );
  while(!open.empty()){
    P tmp = open.front(); open.pop();
    int cnt = tmp.first;
    vector<int> vec = tmp.second;
    if(solve(vec)){
      cout <<cnt <<endl;
      break;
    }
    if(S.find(vec) != S.end()) continue;
    S.insert(vec);
    FOR(i, 1, vec.size()){
      if(vec[i] == vec[i - 1]) continue;
      swap(vec[i], vec[i - 1]);
      open.push( P(cnt + 1, vec) );
      swap(vec[i], vec[i - 1]);
    }
  }
  return 0;
}