Copying code

3.9

15 votes
, Basic Math, Algorithms, Mathamatics, Math
Problem

Y has n lines of code and want to move the nth line of code to the ith line of code.
Y can use two methods for this work:

  • First, Y can press Ctr + x (two keys) and cut the nth line of code, then move the cursor to the ith line pressing up key ni times and then paste the nth line using Ctr + v (two keys).
  • Second, Y can press Ctr + up (two keys) ni times.

What is the minimum number of keys that Y has to press?

Input
The only line of input is n and i separated by space.

1in109

Output

A single line, the minimum number of keys that Y has to press to do the job.

Sample Input
5 1
Sample Output
8
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Y can do both methods and press 8 keys.

  • Press Ctr + x and then press up 4 times and at the end press Ctr + v (two times Ctr, one time x, v and  4 time up that sums up to 8).
  • Press Ctr + up 4 times (both Ctr and up pressed 4 times that sums up to 8).
Editor Image

?