Tommy is a spoilt tomcat which receives many toys. The newest of them is made of several neighbouring circular race tracks which can rotate in any direction. The effort that Tommy makes when it rotates a track is equal to the number of degrees by which the track is turned.
Each track is divided into two areas, a red one and a black one. Tommy has to rotate the tracks so that there is a radius which starts from the centre of the toy and touches the red area of each track.
For example, the image in figure 1 represents a toy with tracks. For obtaining a radius which starts from the centre of the toy and touches the red area of each track, Tommy can rotate the second track clockwise and the first track counter-clockwise (figure 2).
Like any tomcat, Tommy wants to obtain the result by making a minimum effort.
Task
Knowing the number of tracks and the way in which the colours are arranged on each of the tracks, determine the minimum effort that Tommy has to make for obtaining a radius which starts from the centre of the toy and touches the red area of each track.
Input data
The first line contains a natural number and each of the following lines contains two distinct natural numbers and , separated by a space, which represent, counter-clockwise, the ends of the red coloured area on that track.
Output data
Display a single natural number representing the minimum effort which Tommy has to make in order to obtain a radius that starts from the centre of the toy and touches the red area of each track.
Constraints and clarifications
- The tracks are numbered from the inside towards the outside and are divided in sub-areas with the same surface, marked like in figure 3.
- For specifying a red coloured area, the degree of the point where the first area starts and the degree of the point where its last sub-area ends are indicated. The red coloured surface begins in the first sub-area and continues, counter-clockwise, until the last sub-area, including it. The example in figure 4 corresponds to a red coloured area which begins at degrees and ends at degrees.
- The rotation of a track is made by one degree at a time (fractions of degrees are not admitted).
- The scoring will be different from the one used at the original contest. Namely, the tests will be grouped in subtasks.
- The test cases are not the same as the ones used in the contest, so the scores might be slightly different from these obtained in the real contest.
- If you use C++, it is recommended to add
cin.tie(0)->sync_with_stdio(0);
at the beggining of themain
function to make the input stream faster.
# | Points | Constraints |
---|---|---|
0 | 0 | Examples. |
1 | 40 | |
2 | 20 | |
3 | 40 | No additional constraints. |
Example
stdin
4
120 270
0 70
240 340
270 90
stdout
90
Explanation
The track two rotates clockwise by degrees, while the first track spins by degrees counterclockwise.