PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000247. Lập trình xuyên đêm (Burning Midnight Oil)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Lập trình xuyên đêm (Burning Midnight Oil)

Burning Midnight Oil

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Vasya must write at least nn lines of code overnight. He writes vv lines in the first session, takes a tea break, and writes ⌊v/k⌋\lfloor v/k\rfloor lines in the next session. Session j+1j+1 produces ⌊v/kj⌋\lfloor v/k^j\rfloor lines for j≥0j\ge0. He falls asleep when a session would produce zero lines and writes no more. Find the smallest positive integer vv that allows him to finish at least nn lines.

Input

The only line contains integers n,kn,k: the required number of lines and the productivity reduction factor.

Output

Print the smallest positive integer vv meeting the requirement.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le1000; 2≤k≤102\le k\le10.
  • Subtask 2 (30 points): 1≤n≤1061\le n\le10^6; 2≤k≤102\le k\le10.
  • Subtask 3 (50 points): 1≤n≤1091\le n\le10^9; 2≤k≤102\le k\le10.

Examples

Input

7 2

Output

4

Explanation

For v=4v=4, the sessions produce 4, 2, and 1 lines, totaling 7. For v=3v=3, the total is only 3+1=43+1=4. Since increasing vv never decreases the total, the answer is 4.

Input

59 9

Output

54

Explanation

With v=54v=54, the total is 54+6=6054+6=60. With v=53v=53, it is only 53+5=5853+5=58. Thus the minimum is 54.