n = gets.to_i
s_str = gets.strip
s = s_str.to_i

slen = s.size

v = 1
12.times {
  kari = (slen/v.to_f).ceil * 10 ** v
  if (kari > s) or (n < 10 ** v)
    break
  end
  v += 1
}
#p v


startv = 10 ** (v - 1) + 1
endv = 10 ** v

def f(b,n)
  if n<b
    return n
  else
    return (f(b,n/b) + n % b)
  end
end

#p startv
#p endv
startv.upto(endv) { |b|
  #puts "b=#{b}"
  if f(b,n) == s
    puts "#{b}"
    exit
  end
}

if (n > 1)
  b = n
  while (b > n - 10)
    #puts "b2=#{b}"
    if f(b,n) == s
      puts "#{b}"
      exit
    end
  end
end
puts "-1"
