PROGRAM ATCODER

implicit none
integer(8) :: n,s,f,n_d,t,i,j,v1,v10,b
real(8) :: n_root

read(*,*) n,s

if(n<s)then
  write(*,'(i0)') -1
  stop
end if

if(n == s)then
  write(*,'(i0)') n+1
  stop
end if

n_root = real(n)**(0.5)
n_d = int(n_root)

do i = 2, n_d
  f = 0
  t = n
  do
    f = f + mod(t,i)
    if(t < i)exit
    t = t/i
  end do
  if(f == s)then
    write(*,'(i0)') i
    stop
  end if
end do

do v10 = 1, n_d
  if(mod(n-s,v10) /= 0)cycle
  b = (n-s)/v10 + 1
  f = mod(n,b) + n/b
  if(f == s)then
    write(*,'(i0)') b
    stop
  end if
end do

write(*,'(i0)') -1

contains
!!!!!! サブルーチンは↓

!!!!!!

END PROGRAM ATCODER
