N = gets.chomp.to_i
x = gets.chomp.split('')

memo = {}
cou = x.count('1')

N.times do |n|
  xx=x.dup
  xx[n] = xx[n]=='0' ? '1' : '0'
  se = xx.join('').to_i(2)

  t = se
  tmp = 0
  while t != 0
    if memo[t]
      memo[se] = memo[t] + tmp
      break
    end
    ni = t.to_s(2)
    if tmp == 0
      if xx[n] == '1'
        co = cou + 1
      else
        co = cou - 1
      end
    else
      co = ni.count('1')
    end
    t = t%co
    tmp += 1
    if t == 0
      memo[se] = tmp
      break
    end
  end
  p memo[se]
end
