import sys import numpy as np from scipy.sparse import csr_matrix from scipy.sparse.csgraph import dijkstra input = sys.stdin.buffer.readline read = sys.stdin.buffer.read N, M = map(int, input().split()) uvcds = list(map(int, read().split())) us = uvcds[0::4] vs = uvcds[1::4] cs = uvcds[2::4] ds = uvcds[3::4] uv2i = {(min(u, v), max(u, v)): i for i, (u, v) in enumerate(zip(us, vs))} costs = cs * 2 starts = us + vs ends = vs + us graph = csr_matrix((costs, (starts, ends)), shape=(N + 1, N + 1)) dist_vec, predecessor = dijkstra(graph, directed=False, indices=1, return_predecessors=True) predecessor = list(predecessor) ans = int(np.round(dist_vec[N])) v = N while v != 1: p = predecessor[v] if p > v: p, v = v, p i = uv2i[(p, v)] costs[i] = costs[i + M] = ds[i] v = p # graph = csr_matrix((costs, (starts, ends)), shape=(N + 1, N + 1)) ans += int(np.round(dijkstra(graph, directed=False, indices=N)[1])) print(ans)