main = do [a, b] <- map (read :: String -> Int) . words <$> getLine let l = iterate (\n -> a * n + b) 0 let ans = length $ takeWhile (/= 0) (take 50000 $ tail l) print $ if ans == 50000 then -1 else ans + 1