(define n (read))
(define p (read))

(define (ruijo n m)
  (cond ((= m 0) 1)
        ((= m 1) n)
        ((= (modulo m 2) 0)
         (let ((sub (ruijo n (quotient m 2)))) (* sub sub)))
        (#t (let ((sub (ruijo n (quotient m 2)))) (* sub sub n)))))

(define (ans x max)
  (let ((jo (ruijo x n)))
    (cond ((> jo p) max)
          ((= (modulo p jo) 0) (ans (+ x 1) x))
          (#t (ans (+ x 1) max)))))

(display (cond ((= n 1) p)
               (#t (ans 2 1))))
(newline)