A, B = gets.split.map(&:to_i) if A != -1 puts -1 elsif n = B res = 1 while n != 0 n = A * n + B res += 1 end puts n end