#C6555. Minimum Moves to Reach Position N

    ID: 50328 Type: Default 1000ms 256MiB

Minimum Moves to Reach Position N

Minimum Moves to Reach Position N

You are given two integers (N) and (K). Starting from position 0, you can make either of two moves:

  1. Move left by 1 unit (i.e., go from (p) to (p-1)).
  2. Move right by (K) units (i.e., go from (p) to (p+K)).

The objective is to determine the minimum number of moves required to reach exactly the position (N). For example, if (N=5) and (K=3), one optimal sequence of moves is: 0 (\to) -1 (\to) 2 (\to) 5, which takes 3 moves.

A breadth-first search (BFS) approach is recommended for exploring the possible positions efficiently.

inputFormat

The input is read from STDIN and consists of two space-separated integers:

(N): The target position (an integer). (K): The step size for the right move (an integer).

outputFormat

Print to STDOUT a single integer representing the minimum number of moves required to reach the position (N).## sample

5 3
3