Tommy's Toy

Time limit: 0.09s Memory limit: 32MB Input: Output:

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 44 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 nn and each of the following nn lines contains two distinct natural numbers xx and yy, 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

  • 2n500 0002 \leq n \leq 500\ 000
  • 0x,y<360,xy0 \leq x,y < 360, x \neq y
  • The tracks are numbered from the inside towards the outside and are divided in 360360 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 270270 degrees and ends at 6060 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 the main function to make the input stream faster.
# Points Constraints
0 0 Examples.
1 40 n10 000n \le 10\ 000
2 20 n100 000n \le 100\ 000
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 2020 degrees, while the first track spins by 7070 degrees counterclockwise.

Log in or sign up to be able to send submissions!