f b n | n < b     = n
      | otherwise = f b (n `div` b) + (n `mod` b) 
main = do n <- fmap read getLine
          s <- fmap read getLine
          let m = floor $ sqrt $ fromIntegral n
              xs = filter ((== s) . flip f n) [2..m]
              ys = filter ((== s) . flip f n)
                   $ map (((n - s) `div`) . (+ 1)) [2..m]
          case (xs,ys) of 
            _ | n == s -> print (-1)
            (x:_,_)    -> print x
            (_,y:_)    -> print y