1038_E. Maximum Matching
You are given n blocks, each of them is of the form [color_1|value|color_2], where the block can also be flipped to get [color_2|value|color_1]. 

A sequence of blocks is called valid if the touching endpoints of neighboring blocks have the same color. For example, the sequence of three blocks A, B and C is valid if the left color of the B is the same as the right color of the A and the right color of the B is the same as the left color of C.

The value of the sequence is defined as the sum of the values of the blocks in this sequence.

Find the maximum possible value of the valid sequence that can be constructed from the subset of the given blocks. The blocks from the subset can be reordered and flipped if necessary. Each block can be used at most once in the sequence.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of given blocks.

Each of the following n lines describes corresponding block and consists of color_{1,i}, value_i and color_{2,i} (1 ≤ color_{1,i}, color_{2,i} ≤ 4, 1 ≤ value_i ≤ 100 000).

Output

Print exactly one integer — the maximum total value of the subset of blocks, which makes a valid sequence.

Examples

Input

6
2 1 4
1 2 4
3 4 4
2 8 3
3 16 3
1 32 2


Output

63

Input

7
1 100000 1
1 100000 2
1 100000 2
4 50000 3
3 50000 4
4 50000 4
3 50000 3


Output

300000

Input

4
1 1000 1
2 500 2
3 250 3
4 125 4


Output

1000

Note

In the first example, it is possible to form a valid sequence from all blocks.

One of the valid sequences is the following:

[4|2|1] [1|32|2] [2|8|3] [3|16|3] [3|4|4] [4|1|2]

The first block from the input ([2|1|4] → [4|1|2]) and second ([1|2|4] → [4|2|1]) are flipped.

In the second example, the optimal answers can be formed from the first three blocks as in the following (the second or the third block from the input is flipped):

[2|100000|1] [1|100000|1] [1|100000|2]

In the third example, it is not possible to form a valid sequence of two or more blocks, so the answer is a sequence consisting only of the first block since it is the block with the largest value.
```python
def min(a, b):
    if a < b:
        return a
    return b
def max(a,b):
    return abs(min(-a,-b))
been = [0 for i in range(4)]
ans = 0
minw = 10**18
degpar = [0 for i in range(4)]
w = [0 for i in range(4)]
gr = [list() for i in range(4)]
rem = [[0 for i in range(4)] for j in range(4)]
def dfs(x, l):
    l.append(w[x])
    if been[x]:
        return
    been[x] = 1
    for i in gr[x]:
        if been[i] == 0 and rem[x][i] > 0:
            dfs(i, l)
n = int(input())
blocks = []
for i in range(n):
    a,v,b = map(int, input().split())
    a -= 1
    b -= 1
    if a != b:
        blocks.append([a,v,b])
    w[b] += v
    if a != b:
        minw = min(minw, v)
    degpar[a] += 1
    degpar[a] %= 2
    degpar[b] += 1
    degpar[b] %= 2
    gr[a].append(b)
    gr[b].append(a)
    rem[a][b] += 1
    rem[b][a] += 1
ok = True
for i in range(4):
    l = []
    if been[i] == 0:
        dfs(i, l)
    if i == 0 and all(been) and all(degpar):
        ok = False
        break
    ans = max(ans, sum(l))
if ok:
    print(ans)
    exit()
for p in blocks:
    i = p[0]
    j = p[2]
    w[j] -= p[1]
    been = [0 for i in range(4)]
    rem[i][j] -= 1
    rem[j][i] -= 1
    #print(i)
    #print(j)
    #print(':')
    for x in range(4):
        l = []
        if been[x] == 0:
            dfs(x,l)
        ans = max(ans, sum(l))
        #print(sum(l))
    w[j] += p[1]
    rem[i][j] += 1
    rem[j][i] += 1
print(ans)

```