Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <string>
#include <queue>
#include <utility>
#include <algorithm>
#include <set>
using namespace std;

const int INF = (1 << 29);
typedef pair<string ,int> psi;

int main(void) {

  int N, M; cin >> N >> M;
  string b = "";
  for (int i = 0; i < N; i++) {
    int t; cin >> t;
    b += t+'0';
  }
  string t1 = "", t2 = "";
  for (int i = 0; i < M; i++) {
    int p; cin >> p;
    for (int j = 0; j < p; j++) {
      if (i%2) {
        t1 += '0';
        t2 += '1';
      } else {
        t1 += '1';
        t2 += '0';
      }
    }
  }

  int ans1 = INF;
  if (count(t1.begin(), t1.end(), '0') == count(b.begin(), b.end(), '0')) {
    queue<psi> q1;
    set<string> used;
    q1.push(psi(t1,0));
    used.insert(t1);
    while (!q1.empty()) {
      psi s = q1.front(); q1.pop();
      if (s.first == b) {
        ans1 = s.second;
        break;
      }

      for (int i = 0; i < N-1; i++) {
        string tmp = s.first;
        swap(tmp[i], tmp[i+1]);
        if (used.find(tmp) == used.end()) {
          q1.push(psi(tmp, s.second+1));
          used.insert(tmp);
        }
      }
    }
  }

  int ans2 = INF;
  if (count(t2.begin(), t2.end(), '0') == count(b.begin(), b.end(), '0')) {
    queue<psi> q2;
    set<string> used;
    q2.push(psi(t2, 0));
    used.insert(t2);
    while (!q2.empty()) {
      psi s = q2.front(); q2.pop();
      if (s.first == b) {
        ans2 = s.second;
        break;
      }

      for (int i = 0; i < N-1; i++) {
        string tmp = s.first;
        swap(tmp[i], tmp[i+1]);
        if (used.find(tmp) == used.end()) {
          q2.push(psi(tmp, s.second+1));
          used.insert(tmp);
        }
      }
    }
  }

  cout << min(ans1, ans2) << endl;

  return 0;
}