eolymp
bolt
Try our new interface for solving problems
Məsələlər

Combination Lock

Combination Lock

A combination lock consists of a circular dial, which can be turned (clockwise or counterclockwise) and is embedded into the "fixed" part of the lock. The dial has \textbf{N} evenly spaced "ticks". The ticks are numbered from \textbf{0} to \textbf{N}-\textbf{1} , increasing in the clockwise direction. The fixed part of the lock has a "mark" which always "points to" a particular tick on the dial. Of course, the mark points to different ticks as the dial is turned. \includegraphics{https://static.e-olymp.com/content/6f/6fa32832d0d37a3eb6dee21bff2bd19c0c1703d9.jpg} The lock comes with three code numbers \textbf{T1}, \textbf{T2}, \textbf{T3}. These are non-negative integers and each of them is less than \textbf{N}. No two of the three are the same. The lock is opened in three stages of operations: \begin{enumerate} \item Turn the dial clockwise exactly two full revolutions, and continue to turn it clockwise until the mark points to tick \textbf{T1}. \item Turn the dial one full revolution counterclockwise and continue to turn it counterclockwise until the mark points to tick \textbf{T2}. \item Turn the dial clockwise until the mark points to tick \textbf{T3}. The lock should now open. \end{enumerate} You must find the maximum possible number of ticks the dial must be turned in order to open the lock. The number of ticks turned is defined to be the sum of the ticks turned in the three stages outlined above, and is always positive regardless of direction. \InputFile The input file consists of a number of test cases, one test case per line. Each line of the input file contains four integers: \textbf{N}, \textbf{T1}, \textbf{T2}, \textbf{T3}, in this order, separated by blank spaces. The integer \textbf{N} is a multiple of \textbf{5,25} <= \textbf{N} <= \textbf{100} . The numbers \textbf{T1}, \textbf{T2} and \textbf{T3} satisfy the constraints stated under the description above. The input will be terminated by a line with \textbf{N} = \textbf{T1} = \textbf{T2} = \textbf{T3} = \textbf{0} . \OutputFile For each test case, print the maximum possible number of ticks the dial must be turned in order to open the lock. Print each on its own line.
Zaman məhdudiyyəti 1 saniyə
Yaddaşı istafadə məhdudiyyəti 64 MiB
Giriş verilənləri #1
80 20 40 50
80 10 79 12
0 0 0 0
Çıxış verilənləri #1
409
455