#P1282. 多米诺骨牌

多米诺骨牌

Description

A domino consists of two squares, upper and lower. Each square has 11 to 66 pips. For a row of dominoes, let S1S_1 be the sum of pips in the upper squares and S2S_2 be the sum of pips in the lower squares. Their difference is S1S2\left|S_1 - S_2\right|. As shown in the figure, S1=6+1+1+1=9S_1 = 6+1+1+1 = 9, S2=1+5+3+2=11S_2 = 1+5+3+2 = 11, and S1S2=2\left|S_1 - S_2\right| = 2. Each domino can be rotated by 180°180° so that the upper and lower squares swap positions. Please compute the minimum number of rotations needed to make the difference between the two rows as small as possible.

For the example in the figure, rotating only the last domino by 180°180° makes the difference between the two rows equal to 00.

Input Format

The first line contains a positive integer nn (1n10001 \le n \le 1000), the number of dominoes. Each of the next nn lines contains two positive integers aa and bb (1a,b61 \le a, b \le 6), representing the pips on the upper and lower squares of a domino.

Output Format

Output a single line containing one integer: the minimum number of rotations.

4
6 1
1 5
1 3
1 2

1

Hint

Translated by ChatGPT 5