Time limit
2s
Memory limit
128 MB
Cheonsu is playing a stacking game with several kinds of dice. Every die is a cube of the same size, and its six faces contain the numbers 1 through 6, one number on each face. Unlike an ordinary die, the numbers on opposite faces do not necessarily add up to 7.
The dice are stacked from bottom to top in order: die 1, die 2, die 3, and so on. Whenever two dice touch, the number on the upper face of the lower die must be equal to the number on the lower face of the upper die. In other words, the upper face of die 1 must match the lower face of die 2, the upper face of die 2 must match the lower face of die 3, and so on. Die 1 may be placed in any orientation.
After the dice are stacked, they form a long rectangular column with four vertical side faces. Each die may be rotated sideways by 90, 180, or 270 degrees while keeping its upper and lower faces fixed. Therefore, once the top and bottom faces of a die are fixed, any one of its four side numbers can be placed on the side face being considered. Find the maximum possible sum of the numbers on one of the four vertical side faces.
The first line contains the number of dice, N. Each of the next N lines describes one die, from die 1 to die N in order. The six numbers on a line are given in the order A, B, C, D, E, F from the net below. In this net, the opposite pairs are A-F, B-D, and C-E.
N is at most 10,000. The same kind of die may appear more than once.

Figure 1
Print the maximum possible sum of the numbers on one vertical side face.
