import Data.Bits
import Data.Array
main = do
  n <- read  <$> getLine
  x' <- getLine
  let x = listArray (0,n) x'
      p = length $ filter (=='1') x'
      r0 = sum [if x ! i == '1' then modPow 2 (n-1-i) (p+1) else 0| i <- [0..n-1]]
      r1 = if p /= 1 
        then sum [if x ! i == '1' then modPow 2 (n-1-i) (p-1) else 0| i <- [0..n-1]]
        else undefined 
      ans = fmap (print . f) (solve n x r0 r1 p)
  sequence ans

solve :: Int -> Array Int Char -> Int -> Int -> Int -> [Int]
solve n x r0 r1 p =
  [if x ! i == '1' && p /= 1 then 
    (r1 - modPow 2 (n-1-i) (p-1)) `mod` (p-1) 
  else if x ! i == '1' && p == 1 then -1
    else (r0 + modPow 2 (n-1-i) (p+1)) `mod` (p+1) | i <- [0..(n-1)]]

modPow :: Int -> Int -> Int -> Int
modPow x n p -- x^n mod p
  | n == 0 = 1 `mod` p
  | even n = (t * t) `mod` p
  | odd n = (x * modPow x (n-1) p) `mod` p
    where t = modPow x (n `div` 2) p

f :: Int -> Int
f n
  | n == -1 = 0
  | n == 0 = 1
  | otherwise = 1 + f n'
  where n' = n `mod` popCount n

