Combination Lock#536

Combination Lock

1 second
64 megabytes
Easy

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 evenly spaced "ticks". The ticks are numbered from to N-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. The mark points to different ticks as the dial is turned.

The lock comes with three code numbers: T_1, T_2, T_3. These are non-negative integers, each less than . No two of the three are the same.

The lock is opened in three stages:

  1. Turn the dial clockwise exactly two full revolutions, and continue to turn it clockwise until the mark points to tick T_1.

  2. Turn the dial one full revolution counterclockwise and continue to turn it counterclockwise until the mark points to tick T_2.

  3. Turn the dial clockwise until the mark points to tick T_3. The lock should now open.

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 as the sum of the ticks turned in the three stages outlined above, and is always positive regardless of direction.

Input

The input file consists of several test cases, one per line. Each line contains four integers: , T_1, T_2, T_3 (in this order, separated by blank spaces). The integer is a multiple of (25 \leq N \leq 100). The numbers T_1, T_2, and T_3 satisfy the constraints stated above. The input is terminated by a line with N = T_1 = T_2 = T_3 = 0.

Output

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 a separate line.

Examples

Example 1

Loading
Loading