Under construction

Still being built. Things may change

Shortest Pilgrimage

Time limit5sMemory limit1024 MB

Summary
Find the shortest simple path from site 1 to site N that visits exactly eight distinct sites on a graph with weighted undirected roads.
Level

Medium7 of 10

Topics
Dynamic programming, Graph, Bit manipulation
Solved
No attempts yet

Problem

A pilgrim keeps making a pilgrimage that seems like it will never end. There are NN holy sites on the earth, numbered from 1 to NN. There are MM roads, and road ii lets you travel between sites x_ix\_i and y_iy\_i in w_iw\_i minutes.

The pilgrim plans a new pilgrimage that starts at site 1 and ends at site NN. The pilgrimage visits exactly eight different sites, including sites 1 and NN. No site may be visited more than once.

Considering only the travel time on the roads, find the shortest time the pilgrimage takes.

Input

The first line contains the number of sites NN and the number of roads MM.

Each of the next MM lines contains road ii as x_ix\_i, y_iy\_i, w_iw\_i (1≤x_i,y_i≤N1 \leq x\_i,y\_i \leq N, x_i≠y_ix\_i \neq y\_i, 1≤w_i≤1081 \leq w\_i \leq 10^8). The same pair of sites is never connected by more than one road.

Output

Print the shortest time of the pilgrimage in minutes on the first line. If no pilgrimage satisfies the conditions, print -1.

Examples2

  1. Example 1

    Input
    10 10
    1 2 2
    2 3 2
    3 4 2
    4 5 2
    5 6 2
    6 7 2
    7 10 2
    3 8 1
    8 9 5
    9 6 1
    
    Expected output
    14
    
  2. Example 2

    Input
    8 8
    1 2 1
    2 3 1
    3 4 1
    4 8 1
    1 5 1
    5 6 1
    6 7 1
    7 8 1
    
    Expected output
    -1