require 'set'

n, m = gets.split.map(&:to_i)
s = gets.split.join.to_i(2)
g = gets.split.map(&:to_i)

queue = [s]
reached = Set[s]
step = 0

loop do
    new_queue = []
    until queue.empty?
        x = queue.pop
        if sprintf("%0#{n}b",x).scan(/0+|1+/).map(&:size) == g
            p step
            exit
        end

        (0..n-2).each do |i|
            next if [0b11, 0b00].include?((x >> i) & 0b11)
            y = x ^ (0b11 << i)
            next if reached.include?(y)
            reached << y
            new_queue << y
        end
    end
    queue = new_queue
    step += 1
    break if queue.empty?
end