import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /* First Dijkstra Then DFS Should be AC, but just hits the 1s bound */ public class JoshuaDetourDFS { int n, m; int AMSTERDAM = 1; int DELFT = 0; // index -> adjecency list with two components List[] adj_w; int[] parents; int[] parents2; boolean[] seendfs; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { (new JoshuaDetourDFS()).solve(); } void dijkstra(int s) { parents = new int[n]; Arrays.fill(parents, -1); boolean[] seen = new boolean[n]; Arrays.fill(seen, false); int[] dist = new int[n]; Arrays.fill(dist, Integer.MAX_VALUE); PriorityQueue work = new PriorityQueue<>(Comparator.comparingInt(o -> o[1])); dist[s] = 0; work.add(new int[]{s, dist[s]}); while (!work.isEmpty()) { int[] curr = work.poll(); int currn = curr[0]; if (seen[currn]) continue; seen[currn] = true; for (int[] tw : adj_w[currn]) { int t = tw[0]; if (seen[t]) continue; int newd = dist[currn] + tw[1]; if (newd < dist[t]) { dist[t] = newd; parents[t] = currn; work.add(new int[]{t, dist[t]}); } } } } boolean dfs(int s, int s2) { seendfs[s] = true; if(s == s2) return true; boolean found = false; for (int[] tw : adj_w[s]) { int t = tw[0]; if (parents[s] == t) continue; // NOT ALLOWED! if (seendfs[t]) continue; parents2[t] = s; found |= dfs(t, s2); if(found) break; } return found; } void solve() throws IOException { String[] words = br.readLine().split("\\s"); n = Integer.parseInt(words[0]); m = Integer.parseInt(words[1]); adj_w = new ArrayList[n]; for (int i = 0; i < n; i++) { adj_w[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { words = br.readLine().split("\\s"); int a = Integer.parseInt(words[0]); int b = Integer.parseInt(words[1]); int d = Integer.parseInt(words[2]); adj_w[a].add(new int[]{b, d}); adj_w[b].add(new int[]{a, d}); } dijkstra(AMSTERDAM); parents2 = new int[n]; Arrays.fill(parents2, -1); seendfs = new boolean[n]; Arrays.fill(seendfs, false); dfs(DELFT, AMSTERDAM); if (parents2[AMSTERDAM] == -1) { System.out.println("impossible"); return; } else { List sol = new ArrayList<>(); int n = AMSTERDAM; while (n != -1) { sol.add(n); n = parents2[n]; } System.out.print(sol.size()); Collections.reverse(sol); for (int x : sol) { System.out.print(" " + x); } System.out.println(); return; } } }