@staticmethod
def c():
    (n, m, *uvl) = map(int, sys.stdin.read().split())
    (u, v, l) = np.array(uvl).reshape(m, 3).T
    u -= 1
    v -= 1
    g = csgraph_to_dense(csr_matrix((l, (u, v)), (n, n)))
    g += g.T
    g[(g == 0)] = np.inf
    dist0 = g[0].copy()
    g[0] = 0
    g[:, 0] = 0
    dist = shortest_path(g, method='FW', directed=False)
    (u, v) = np.array([*itertools.combinations(range(1, n), 2)]).T
    res = ((dist0[u] + dist[(u, v)]) + dist0[v]).min()
    print(((- 1) if (res == np.inf) else int(res)))
@staticmethod
def c():
    (n, m, *uvl) = map(int, sys.stdin.read().split())
    (u, v, l) = np.array(uvl).reshape(m, 3).T
    u -= 1
    v -= 1
    g = csgraph_to_dense(csr_matrix((l, (u, v)), (n, n)))
    g += g.T
    g[(g == 0)] = np.inf
    dist0 = g[0].copy()
    g[0] = 0
    g[:, 0] = 0
    dist = shortest_path(g, method='FW', directed=False)
    (u, v) = np.array([*itertools.combinations(range(1, n), 2)]).T
    res = ((dist0[u] + dist[(u, v)]) + dist0[v]).min()
    print(((- 1) if (res == np.inf) else int(res)))