import java.util.Scanner; class main{ public static void main(String args[]){ Scanner scan = new Scanner(System.in); int A = scan.nextInt(); int B = scan.nextInt(); int ans; if(A < B){ ans = (A - 1) + (B - A - 1); } else{ ans = (2000000000 - A) + (A - B - 1); } System.out.println(ans); } }