# 2019 XCPC Newbie Selection #1 (South Pacific ICPC Regional Finals 2018)

E. Explosive Wiring
PDF 题面可用

### 样例

Input
1
1 3 0 0 1 1 2 0

Output
0

Input
5
1 3 0 0 50 50 100 0
1 3 45 0 50 40 110 0
1 3 10 0 20 10 30 0
1 3 20 0 30 10 40 0
1 3 35 0 45 10 50 0

Output
4

Input
5
1 3 0 0 50 50 100 0
1 3 45 0 50 40 110 0
1 3 10 0 20 10 30 0
1 3 20 0 30 10 40 0
10 3 35 0 45 10 50 0

Output
11


NaN