Log inSign up
Problems / B078

Setting the dial

200 ptsCases 15Set by dongbinna
Added October 30.

Statement

A signal generator in a lab has one dial on it, and the dial carries marks numbered from 0 to 100,000. The dial is set to mark N right now, and the experiment needs it set to mark K.

There are only three ways to move the dial. When the current mark is X, the mark each way moves to is shown in the table below.

WayDown by 1Up by 1Double
Mark moved toX-1X+12X

Any one of the moves takes 1 second. None of the three ways can reach a mark below 0 or a mark above 100,000. A move that would land on such a mark counts as unusable. A mark that has already been visited may be passed through again, and the dial may overshoot the target mark and come back.

Write a program that computes the shortest time it takes to set the dial from mark N to mark K.

For example, suppose N=5 and K=17. The four moves below reach mark 17.

Time0 seconds1 second2 seconds3 seconds4 seconds
Way usedNoneDown by 1DoubleDoubleUp by 1
Mark5481617

Doubling straight from mark 5 gives mark 10, turning up by 1 from there gives mark 11, and doubling again gives mark 22, which overshoots mark 17. Turning down by 1 from there costs five more moves. No way of reaching mark 17 takes three moves or fewer. The answer for this example is therefore 4 seconds.

Input and Output Examples

Example 1

Input

5 17

Output

4

Example 2

Input

1 1

Output

0

Example 3

Input

1 3

Output

2

Input

  • First the mark N the dial is set to right now and the mark K it has to be set to are given on one line, separated by a space. N and K are each a natural number from 1 to 100,000. N and K may be equal. The marks the dial can pass through run from 0 to 100,000, so passing through mark 0 along the way is allowed. The input ends with this one line.

Output

  • Print the shortest time it takes to set the dial from mark N to mark K as a number of seconds on one line. If the dial is already set to mark K and does not need to move, print 0.

Requirements

  • One move can turn the mark down by 1, up by 1, or to twice its value
  • Any one of the ways takes 1 second per move
  • The dial cannot reach a mark below 0 or a mark above 100,000
  • Print only the single shortest time it takes
  • You must write a program that reads from standard input and writes to standard output
  • Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
  • The languages you can use are Python and JavaScript
  • The memory limit is 256MB. The browser does not measure it, it only states it
  • The time limit is set by measuring the setter's reference solution on the same runtime
  • On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
  • The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
  • There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
  • You have to pass all 15 cases to solve this.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
5 17
4
1 1
0
1 3
2

Solution

Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.

0 chars1 lineMemory 256MB (recorded only)Reference 20ms

Submitting requires you to log in. Running against the public cases works without one.

You can test with 3 public cases. Submitting runs the hidden cases too.

People who solved this problem

Nobody yet. You could be the first.