import sys
import heapq

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    idx += 1
    M = int(input[idx])
    idx += 1
    
    adj = [[] for _ in range(N+1)]
    for _ in range(M):
        A = int(input[idx])
        idx += 1
        B = int(input[idx])
        idx += 1
        C = int(input[idx])
        idx += 1
        X = int(input[idx])
        idx += 1
        adj[A].append((B, C, X))
        adj[B].append((A, C, X))
    
    INF = 1 << 60
    d0 = [INF] * (N + 1)
    d1 = [INF] * (N + 1)
    d0[N] = 0
    heap = []
    heapq.heappush(heap, (0, N, 0))
    
    while heap:
        dist, u, state = heapq.heappop(heap)
        if state == 0:
            if dist > d0[u]:
                continue
        else:
            if dist > d1[u]:
                continue
        
        for (v, c, x) in adj[u]:
            if state == 0:
                new_dist0 = d0[u] + c
                if x == 1:
                    if new_dist0 < d1[v]:
                        d1[v] = new_dist0
                        heapq.heappush(heap, (d1[v], v, 1))
                else:
                    if new_dist0 < d0[v]:
                        d0[v] = new_dist0
                        heapq.heappush(heap, (d0[v], v, 0))
            if state == 1:
                new_dist1 = d1[u] + c
                if new_dist1 < d1[v]:
                    d1[v] = new_dist1
                    heapq.heappush(heap, (d1[v], v, 1))
    
    for i in range(1, N):
        print(d1[i] if d1[i] != INF else -1)

if __name__ == '__main__':
    main()