Example

Input

6 3
1 0 0 1 0 1
1 3 2


Output

1
#include <iostream>
#include <algorithm>
using namespace std;

int search_bit(int a[], int from, int b[], int n) {
  for (int i = from; i < n; i++) {
    if (b[i] == a[from]) return i;
  }
  return -1;
}

void move(int a[], int from, int to) {
  int temp = a[to];
  for (int i = from + 1; i <= to; i++) {
    a[i] = a[i - 1];
  }
  a[from] = temp;
}

void display(int a[], int n) {
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
  cout << endl;
}

int operate(int init[], int run[], int n) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    //display(run, n);
    if (init[i] == run[i]) continue;
    int match = search_bit(init, i, run, n);
    count += match - i;
    move(run, i, match);
  }
  //display(run, n);
  return count;
}

int count_bit(int a[], int n) {
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (a[i]) count++;
  }
  return count;
}

int main() {
  int n, m;
  cin >> n >> m;
  int b[n], p[m];
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> p[i];
  }
  int run0[n], run1[n];
  int pos = 0;
  int bit = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < p[i]; j++) {
      run0[pos++] = bit;
    }
    bit = 1 - bit;
  }
  for (int i = 0; i < n; i++) {
    run1[i] = (run0[i]) ? 0 : 1;
  }
  int count0 = -1, count1 = -1;
  int res[n];
  if (count_bit(b, n) == count_bit(run0, n)) {
    count0 = operate(b, run0, n);
  }
  if (count_bit(b, n) == count_bit(run1, n)) {
    count1 = operate(b, run1, n);
  }
  if (count0 == -1) {
    cout << count1 << endl;
  } else if (count1 == -1) {
    cout << count0 << endl;
  } else {
    cout << min(count1, count0) << endl;
  }
  return 0;
}

