Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using pii = pair<int, int>;

template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>>& G, T INF) {
  using P = pair<T, int>;
  int n = G.size();
  vector<T> d(n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.emplace(0, s);
  while (que.size()) {
    P p = que.top(); que.pop();
    int u = p.second;
    if (d[u] < p.first) continue;
    for (auto& e : G[u]) {
      int v = e.first;
      if (d[v] <= d[u] + e.second) continue;
      d[v] = d[u] + e.second;
      que.emplace(d[v], v);
    }
  }
  return d;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  
  int n, m;
  cin >> n >> m;

  vector<int> b(n), p(m);
  int one = 0, sum = 0, s = 0;
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    one += b[i];
    s += (b[i] << (n - i - 1));
  }
  for (int i = 0; i < m; i++) {
    cin >> p[i];
    if ((i & 1) == 0) sum += p[i];
  }

  vector<int> goal;
  if (one == sum) {
    int g = 0, shift = n - 1;
    for (int i = 0; i < m; i++) {
      if ((i & 1) == 0) for (int j = 0; j < p[i]; j++) g |= 1 << (shift - j);
      shift -= p[i];
    }
    goal.emplace_back(g);
  }
  if (n - one == sum) {
    int g = 0, shift = n - 1;
    for (int i = 0; i < m; i++) {
      if ((i & 1) != 0) for (int j = 0; j < p[i]; j++) g |= 1 << (shift - j);
      shift -= p[i];
    }
    goal.emplace_back(g);
  }

  vector<vector<pii>> G(1 << n);
  for (int i = 0; i < 1 << n; i++) {
    if (__builtin_popcount(i) != one) continue;
    for (int j = 1; j < n; j++) {
      if (((i >> (j - 1)) & 1) ^ ((i >> j) & 1)) {
        int to = i, l = (to >> j) & 1, r = (to >> (j - 1)) & 1;
        to -= (1 << j) * l + (1 << (j - 1)) * r;
        to += (1 << j) * r + (1 << (j - 1)) * l;
        G[i].emplace_back(to, 1);
      }
    }
  }

  auto dis = dijkstra(s, G, 1 << 30);
  int ans = 1 << 30;
  for (int i = 0; i < goal.size(); i++) ans = min(ans, dis[goal[i]]);
  cout << ans << endl;

  return 0;
}
