using System; using System.Collections.Generic; using System.Linq; namespace Test { class Program { static void Main(string[] args) { int[] x = Console.ReadLine().Split().Select(int.Parse).ToArray(); if (x[0] < x[1]) { Console.WriteLine((x[0] - 1) + (x[1] - x[0] - 1)); } else { Console.WriteLine((2000000000 - x[0]) + (x[0] - x[1] - 1)); } } } }