Under construction

Still being built. Things may change

Traveling SCCC President 2

Time limit3sMemory limit1024 MB

Summary
Find the path from building 1 to building N whose road lengths have the smallest bitwise OR, and print that OR value.
Level

Medium6 of 10

Topics
Bit manipulation, Union-find, Greedy
Solved
No attempts yet

Problem

Sangwon, the president of SCCC, a problem-solving club in the Department of Computer Science and Engineering at Soongsil University, wants to walk from the Information Science Building to the Hyeongnam Engineering Building to put up contest promotion posters.

The Soongsil University campus has NN buildings numbered 11 to NN and MM roads numbered 11 to MM. Each road connects two different buildings. The ii-th road connects building uiu_i and building viv_i, and its length is wiw_i. At most one road connects any pair of buildings, and every building can be reached from every other building by roads. The Information Science Building is building 11, and the Hyeongnam Engineering Building is building NN.

An ordinary person spends time equal to the sum of the road lengths used to travel between buildings. Sangwon, however, spends time equal to the bitwise OR of all the road lengths used. Find the minimum time Sangwon needs to travel from building 11 to building NN.

Input

The first line contains the number of buildings NN and the number of roads MM, separated by a space.

From the second line onward, each of the next MM lines contains the two buildings ui,viu_i, v_i connected by the ii-th road and its length wiw_i, separated by spaces.

Output

Print the minimum time Sangwon needs to travel from building 11 to building NN.

Constraints

  • 2≤N≤300 0002 \le N \le 300\,000
  • N−1≤M≤300 000N-1 \le M \le 300\,000
  • 1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \ne v_i (1≤i≤M)(1 \le i \le M)
  • 0≤wi<2600 \le w_i < 2^{60} (1≤i≤M)(1 \le i \le M)
  • If i≠ji \ne j, then (ui,vi)≠(uj,vj)(u_i, v_i) \ne (u_j, v_j) and (ui,vi)≠(vj,uj)(u_i, v_i) \ne (v_j, u_j). No two roads connect the same pair of buildings.
  • Every building is connected to the others by roads.
  • All input values are integers.

Hint

For two integers AA and BB, the bitwise OR A OR BA\text{ OR }B is defined as follows. In binary, if at least one of the 2k2^k digits of AA and BB is 1, then the 2k2^k digit of A OR BA\text{ OR }B is 1. If both are 0, the 2k2^k digit of A OR BA\text{ OR }B is 0.

For example, 12=1100(2)12=1100_{(2)} and 10=1010(2)10=1010_{(2)}, so 12 OR 10=1100(2) OR 1010(2)=1110(2)=1412\text{ OR }10=1100_{(2)}\text{ OR }1010_{(2)}=1110_{(2)}=14.

The bitwise OR of kk integers A1,A2,⋯ ,AkA_1, A_2, \cdots, A_k is defined as (⋯((A1 OR A2) OR A3) OR ⋯ ) OR Ak(\cdots((A_1\text{ OR }A_2)\text{ OR }A_3)\text{ OR }\cdots)\text{ OR }A_k. You can prove that this result does not change when the order of A1,…,AkA_1, \ldots, A_k is changed.

Road lengths and the answer can exceed the 32-bit integer range, so C/C++ users should use the long long type and Java users should use the long type.

Because the input and output are large, using fast I/O as described in the language guide on pages 2 to 4 of the problem booklet is recommended.

Examples2

  1. Example 1

    Input
    4 4
    1 2 1
    2 3 2
    3 4 3
    1 4 4
    
    Expected output
    3
    
  2. Example 2

    Input
    2 1
    1 2 1152921504606846975
    
    Expected output
    1152921504606846975