D. Cakes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Everyone loves sweets, so it's not surprising that Charlie's chocolate factory is expanding again. This time, it was decided to install a conveyor belt for making cakes. For this purpose, the conveyor will have $$$N$$$ molds for cakes. Above the conveyor are dispensers, each filled with one of three types of filling. Each cake must consist of three different layers, arranged in a specific order. Each layer can be applied using one of the dispensers.

The conveyor can only move to the right. When the molds for the cakes are positioned under the dispensers, layers can be applied to the corresponding molds using the specified dispensers. When all three required layers have been applied to a cake, the cake is considered made. Your task is to load the dispensers so that all cakes can be made in the minimum number of movements of the conveyor.

More formally, there are $$$N$$$ cakes, each defined by a triplet of numbers $$$a$$$, $$$b$$$, $$$c$$$. A cake must sequentially (but not necessarily consecutively) be positioned under the dispensers filled with fillings numbered $$$a$$$, then $$$b$$$, then $$$c$$$ in that order to be considered made. The conveyor can be represented as a number line. At points $$$0$$$, $$$1$$$, etc., above it are the dispensers (you can assume an unlimited number of them). The first cake is located at point $$$-1$$$, the second at $$$-2$$$, and so on, with the $$$N$$$-th cake at point $$$-N$$$. Note that the dispensers are located at non-negative coordinates, while the cakes are at negative ones. You can shift all cakes one unit to the right (increase their coordinates by $$$1$$$). A cake is under a dispenser if it is at the same point.

Your task is to load the dispensers and then make all the cakes with the minimum number of movements of the conveyor, and print this number on the screen.

Input

The first line of the input contains the number $$$N$$$ ($$$1 \leq N \leq 10^5$$$) — the number of cakes. The next $$$N$$$ lines describe the $$$i$$$-th cake with three numbers $$$a$$$, $$$b$$$, $$$c$$$ ($$$1 \leq a, b, c \leq 3, a \neq b \neq c, a \neq c$$$).

Output

The output should contain a single integer — the minimum necessary number of movements of the conveyor.

Examples
Input
2
1 2 3
1 2 3
Output
4
Input
2
2 3 1
1 3 2
Output
5
Input
1
1 2 3
Output
3
Note

In the first test case, it is necessary to load the dispensers ($$$1, 2, 3$$$). In the second example, the sequence would be ($$$2, 1, 3, 2, 1$$$).