A, B = map(int, input().split()) if A < B: print(A - 1 + B - A - 1) else: print(2 * 10 ** 9 - A + A - B - 1)