Problem 1973 --I Wanna Go Home

1973: I Wanna Go Home

Time Limit: 1 Sec  Memory Limit: 32 MB
Submit: 37  Solved: 13
[Submit][Status][Web Board][Creator:][下载FPS1元][添加到购物车][下载测试数据1元][172kb]


The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.
    "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."
    Would you please tell Mr. M at least how long will it take to reach his sweet home?


 The input contains multiple test cases.
    The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
    The second line contains one integer M (0<=M<=10000), which is the number of roads.
    The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
    Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i.
    To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2.
    Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.


For each test case, output one integer representing the minimum time to reach home.
    If it is impossible to reach home according to Mr. M's demands, output -1 instead.

Sample Input

1 2 13
1 2
1 2 63
1 3 55
1 4 31
2 4 18
3 4 69
1 2 2 1

Sample Output