Question
Hostel Corridor Painting

A hostel corridor has N rooms in a row. Each room can be painted red, green, or blue. For every room, the cost of each color is different. To avoid a dull-looking corridor, two adjacent rooms cannot have the same color.

Find the minimum total painting cost.

The twist: choosing the cheapest color for the current room may block a better choice for the next room.

Input

The first line contains an integer KaTeX can only parse string typed expression — the number of rooms.

Each of the next KaTeX can only parse string typed expression lines contains three integers KaTeX can only parse string typed expression, KaTeX can only parse string typed expression, and KaTeX can only parse string typed expression — the cost of painting room KaTeX can only parse string typed expression red, green, and blue.

Output

Print a single integer — the minimum total painting cost.

Example
Example 1:
Input
3

17 2 17
16 16 5
14 3 19
Output
10
Explanation
Paint room 1 green, room 2 blue, and room 3 green.

Example 2:
Input
4

7 6 2
5 8 6
7 3 4
9 2 7
Output
12

Online