A,B = map(int,raw_input().split()) if A < B: print (B - A) + A - 2 else: print A - B + (2000000000 - A + 1) - 2