Under construction

Still being built. Things may change

The Lost Cow

Interview

Time limit2sMemory limit512 MB

Summary
Given start x and target y on a number line, simulate a doubling zig-zag search and report the total distance traveled until y is reached.
Level

Easy3 of 10

Topics
Math, Simulation
Solved
No attempts yet

Problem

Farmer John has lost his prize cow Bessie, and he needs to find her!

Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position xx and Bessie is currently at position yy (unknown to Farmer John). If Farmer John knew where Bessie was located, he could walk directly to her, traveling a distance of ∣x−y∣|x - y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position.

Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature. He is somewhat amused to find that computer scientists have studied this exact problem in the past, and that it is actually called the "Lost Cow Problem" (this is actually true!).

The recommended strategy is to move to position x+1x+1, then reverse direction and move to position x−2x-2, then to position x+4x+4, and so on, in a "zig zag" pattern. Each step moves twice as far from his initial starting position as the step before. As he read during his study of algorithms for the lost cow problem, this approach guarantees that he will at worst travel 9 times the direct distance ∣x−y∣|x-y| before he finds Bessie (this is also true, and the factor of 9 is the smallest worst case guarantee any strategy can achieve).

Farmer John wants to verify this result. Given xx and yy, compute the total distance he travels under the zig-zag search strategy above until he finds Bessie. He stops the first moment he reaches position yy.

Input

The single line of input contains two distinct space-separated integers xx and yy. Both are in the range 00 to 1 0001\,000.

Output

Print one line containing the distance Farmer John travels to reach Bessie.

Examples1

  1. Example 1

    Input
    3 6
    
    Expected output
    9