This page is still under construction.

Parts of this page are still being built. What you see may change.

Deleting Numbers

Time limit1sMemory limit512 MB

Summary
Numbers 1 to n are repeatedly scanned, every k-th remaining number is deleted each round, and you must report which round removes n, or 0 if it survives.
Level

Medium7 of 10

Topics
Math, Simulation, Implementation, Number theory
Solved
No attempts yet

Problem

The natural numbers from 11 to nn are written in a row, and a natural number kk is given.

One or more steps of deleting numbers in this row are performed. At each step, the remaining numbers are scanned in increasing order, and every kk-th number is deleted. If fewer than kk numbers remain after a step, the deletion process ends.

You must determine at which step the number nn is deleted, or find out that it is not deleted before the process ends.

For example, let n=13n = 13 and k=2k = 2.

  • At the first step, the numbers 2,4,6,8,10,122, 4, 6, 8, 10, 12 are deleted, leaving 1,3,5,7,9,11,131, 3, 5, 7, 9, 11, 13.
  • At the second step, the numbers 3,7,113, 7, 11 are deleted, leaving 1,5,9,131, 5, 9, 13.
  • At the third step, the numbers 5,135, 13 are deleted, leaving 1,91, 9.
  • At the fourth step, the number 99 is deleted, leaving 11. Since one number remains, the process ends.

Thus the number 1313 is deleted at the third step.

Write a program that, given the numbers nn and kk, determines at which step the number nn is deleted.

Input

The first line of the input contains an integer nn (3≤n≤10183 \le n \le 10^{18}).

The second line of the input contains an integer kk (2≤k≤1002 \le k \le 100, k<nk < n).

Output

Output a single integer: the number of the step at which the number nn is deleted, or 00 if the number nn is not deleted.

Examples1

  1. Example 1

    Input
    13
    2
    
    Expected output
    3