using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m) = (c[0], c[1]); var map = NArr(m); var tree = new List<(int to, int len)>[n]; var revtree = new List<(int to, int len)>[n]; for (var i = 0; i < n; ++i) { tree[i] = new List<(int to, int len)>(); revtree[i] = new List<(int to, int len)>(); } foreach (var edge in map) { tree[edge[0] - 1].Add((edge[1] - 1, edge[2])); revtree[edge[1] - 1].Add((edge[0] - 1, edge[2])); } var ton2 = BFS(n - 2, revtree); var ton1 = BFS(n - 1, revtree); var fromn2 = BFS(n - 2, tree); var fromn1 = BFS(n - 1, tree); var ans = new long[n - 2]; for (var i = 0; i < ans.Length; ++i) { var len = Math.Min(ton2[i] + ton1[n - 2] + fromn1[i], ton1[i] + ton2[n - 1] + fromn2[i]); ans[i] = len >= INF ? -1 : len; } WriteLine(string.Join("\n", ans)); } static long INF = long.MaxValue / 6; static long[] BFS(int start, List<(int to, int len)>[] tree) { var ans = Enumerable.Repeat(INF, tree.Length).ToArray(); ans[start] = 0; var q = new Queue<(int pos, long len)>(); q.Enqueue((start, 0)); while (q.Count > 0) { var cur = q.Dequeue(); if (ans[cur.pos] != cur.len) continue; foreach (var next in tree[cur.pos]) { if (ans[next.to] <= cur.len + next.len) continue; ans[next.to] = cur.len + next.len; q.Enqueue((next.to, ans[next.to])); } } return ans; } }