object Main extends App {

  import scala.io.StdIn._

  solve
  def solve: Unit = {
    val Array(n, m) = readLine.trim.split(' ')
    if (n == "0") return
    val value = m.toBasedInt(n.toInt)
    println(List(31, 29, 23, 19, 17, 13, 11, 7, 5, 3, 2).filter(n.toInt % _ == 0).map{prime ⇒ countFactorInFactorial(value, prime) / countPrimeFactor(n.toInt, prime)}.min)
    solve
  }
  implicit class ToBase(val str: String) extends AnyVal {
    def toBasedInt(base: Int): Long = {
      str.foldLeft(0L){
          case (a, b) if b.isDigit ⇒ a * base + b.asDigit
          case (a, b) ⇒ a * base + b - 'A' + 10
        }
      }
  }
  def countPrimeFactor(value: Int, prime: Int): Int = {
    value % prime match {
      case 0 ⇒ 1 + countPrimeFactor(value / prime, prime)
      case _ ⇒ 0
    }
  }
  def countFactorInFactorial(value: Long, prime: Long): Long = {
    value match {
      case 0 ⇒ 0
      case _ ⇒ value / prime + countFactorInFactorial(value / prime, prime)
    }
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
    @inline
    def takeIf(@inline predicate: T ⇒ Boolean): Option[T] = Some(value).filter(predicate)
  }
}
