PROGRAM ATCODER

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

read(*,*) n,s

ans = -1

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 = 1, n_d
  f = 0
  t = n
  v10 = i
  if (i/=1) then
    do
      f = f + mod(t,i)
      if(t < i)exit
      t = t/i
    end do
    if(f == s)then
      call chmin(ans,i)
    end if
  end if
  if(n_d**2 == n .and. i == n_d)cycle ! n=s^2の時用のチェック
  b = (n-s)/v10 + 1
  if(mod(n-s,v10) /= 0)cycle
  f = mod(n,b) + n/b
  if(f == s)then
    call chmin(ans,b)
  end if
end do


write(*,'(i0)') ans

contains
!!!!!! サブルーチンは↓
subroutine chmin(dp,x)
implicit none
integer(8) :: dp, x

if(dp < 0) then
  dp = x
  return
end if

if (dp > x) then
  dp = x
  return
end if

end subroutine
!!!!!!

END PROGRAM ATCODER
