import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.prev = current.id;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	StringBuilder sb = new StringBuilder();
            	int previ = graph.get(numVertices-1).prev;
            	sb.append(" ");
            	sb.append(numVertices);
            	sb.append(" ");
            	while(previ != 0){
            		sb.append(previ+1);
            		sb.append(" ");
            		previ = graph.get(previ).prev;
            		//System.out.println(previ);
            	}
            	sb.append(1);
            	sb.reverse();
            	System.out.println(sb.toString());
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.prev = current.id;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
                nodeEnd.neighbors.add(new Edge(nodeStart, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            		//System.out.println(previ);
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i>=0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	StringBuilder sb = new StringBuilder();
            	int previ = graph.get(numVertices-1).prev;
            	sb.append(" ");
            	sb.append(numVertices);
            	sb.append(" ");
            	while(previ != 0){
            		sb.append(previ+1);
            		sb.append(" ");
            		previ = graph.get(previ).prev;
            	}
            	sb.append(1);
            	sb.reverse();
            	System.out.println(sb.toString());
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.prev = current.id;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
                nodeEnd.neighbors.add(new Edge(nodeStart, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ);
            		previ = graph.get(previ).prev;
            		//System.out.println(previ);
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i>=0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        long cost, dist;
        boolean visited;
        List<Integer> nodesInPath;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.dist = Long.MAX_VALUE;
            this.visited = false;
            this.nodesInPath = new LinkedList<>();
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            long ans = graph.get(numVertices-1).dist;
            if (ans == Long.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Long> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add((long) numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == int.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>(numVertices);
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        long cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            long ans = graph.get(numVertices-1).cost;
            if (ans == Long.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = int.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return int.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == int.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        long cost, dist;
        boolean visited;
        List<Integer> nodesInPath;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.dist = Long.MAX_VALUE;
            this.visited = false;
            this.nodesInPath = new LinkedList<>();
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Long.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            long ans = graph.get(numVertices-1).dist;
            if (ans == Long.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Long> answer = new ArrayList<>(ans);
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
		int previ = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
			current.prev = previ;
			previ = current.id;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	List<Integer> answer = new ArrayList<>();
            	int previ = graph.get(numVertices-1).prev;
            	answer.add(numVertices);
            	while(previ != 0){
            		answer.add(previ+1);
            		previ = graph.get(previ).prev;
            	}
            	answer.add(1);
            	for(int i = answer.size() - 1; i >= 0; i--){
            		System.out.print(answer.get(i) + " ");
            	}
            	System.out.println();
            }

    }
}


import java.io.*;
import java.util.*;

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 *
 * I'm getting WA (Wrong Answer) on the online judge. Can you figure out why?
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id, prev;
        List<Edge> neighbors;
        int cost;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Integer.MAX_VALUE;
            this.visited = false;
            this.prev = -1;
        }

        public int compareTo(Node other) {
            return Integer.compare(this.cost, other.cost);
        }
    }

    public static class Edge {
        Node dest;
        int weight;

        public Edge(Node dest, int weight) {
            this.dest = dest;
            this.weight = weight;
        }
    }

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        pq.add(source);
        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                int newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.prev = current.id;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
       final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
                nodeEnd.neighbors.add(new Edge(nodeStart, weight));
            }

            // Convert to 0 indexing

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(0));

            int ans = graph.get(numVertices-1).cost;
            if (ans == Integer.MAX_VALUE) {
                System.out.println(-1);
            } else {
            	StringBuilder sb = new StringBuilder();
            	int previ = graph.get(numVertices-1).prev;
            	sb.append(" ");
            	sb.append(numVertices);
            	sb.append(" ");
            	while(previ != 0){
            		sb.append(previ+1);
            		sb.append(" ");
            		previ = graph.get(previ).prev;
            		//System.out.println(previ);
            	}
            	sb.append(1);
            	sb.reverse();
            	System.out.println(sb.toString());
            }

    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, SimpleEntry<Integer, Integer>> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 0)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            if (!visited.containsKey(vertex) || visited.get(vertex).getValue() > entry.getValue().getValue()) {
                visited.put(vertex, new SimpleEntry<>(entry.getKey(), entry.getValue().getValue()));
            }
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child) + visited.get(vertex).getValue())));
            });
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur).getKey();
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class Pair<A, B> {

        final A left;
        final B right;

        public Pair(A left, B right) {
            this.left = left;
            this.right = right;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Pair<?, ?> pair = (Pair<?, ?>) o;
            return Objects.equals(left, pair.left) &&
                    Objects.equals(right, pair.right);
        }

        @Override
        public int hashCode() {
            return Objects.hash(left, right);
        }
    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        System.out.println(dijkstra(graph, 1, n, n).stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        List<Integer> path = new ArrayList<Integer>();

        if (!graph.containsKey(start)) {
            path.add(-1);
            return path;
        }

        int[] visited = new int[n + 1];
        long[] distances = new long[n + 1];
        Arrays.fill(visited, -1);
        Arrays.fill(distances, Long.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Pair<Integer, Long>> priorityQueue = new PriorityQueue<>((o1, o2) -> {
            if (o1.right < o2.right) {
                return -1;
            } else if (o1.right == o2.right) {
                return 0;
            } else {
                return 1;
            }
        });
        priorityQueue.add(new Pair<>(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            Pair<Integer, Long> pair = priorityQueue.remove();
            int vertex = pair.left;
            long weight = pair.right;

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(new Pair<>(child, distances[child]));
                             });
        }

        if (visited[goal] == -1) {
            path.add(-1);
            return path;
        }

        path.add(goal);
        int cur = goal;
        while (cur != start) {
            cur = visited[cur];
            path.add(cur);
        }

        Collections.reverse(path);
        return path;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class IntPacker {

        public static long packInts(int a, int b) {
            return (((long) a) << 32) | (b & 0xffffffffL);
        }

        public static int getLeft(long pair) {
            return (int)(pair >> 32);
        }

        public static int getRight(long pair) {
            return (int)pair;
        }

    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        System.out.println(dijkstra(graph, 1, n, n).stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        List<Integer> path = new ArrayList<>();

        if (!graph.containsKey(start)) {
            path.add(-1);
            return path;
        }

        int[] visited = new int[n + 1];
        int[] distances = new int[n + 1];
        Arrays.fill(visited, -1);
        Arrays.fill(distances, Integer.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Long> priorityQueue = new PriorityQueue<>((o1, o2) -> IntPacker.getRight(o1) - IntPacker.getRight(o2));
        priorityQueue.add(IntPacker.packInts(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            long pair = priorityQueue.remove();
            int vertex = IntPacker.getLeft(pair);
            int weight = IntPacker.getRight(pair);

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(IntPacker.packInts(child, distances[child]));
                             });
        }

        if (visited[goal] == -1) {
            path.add(-1);
            return path;
        }

        path.add(goal);
        int cur = goal;
        while (cur != start) {
            cur = visited[cur];
            path.add(cur);
        }

        Collections.reverse(path);
        return path;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class IntPacker {

        public static long packInts(int a, int b) {
            return (((long) a) << 32) | (b & 0xffffffffL);
        }

        public static int getLeft(long pair) {
            return (int)(pair >> 32);
        }

        public static int getRight(long pair) {
            return (int)pair;
        }

    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        System.out.println(dijkstra(graph, 1, n, n).stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        int[] visited = new int[n + 1];
        int[] distances = new int[n + 1];
        Arrays.fill(visited, -1);
        Arrays.fill(distances, Integer.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Long> priorityQueue = new PriorityQueue<>((o1, o2) -> IntPacker.getRight(o1) - IntPacker.getRight(o2));
        priorityQueue.add(IntPacker.packInts(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            long pair = priorityQueue.remove();
            int vertex = IntPacker.getLeft(pair);
            int weight = IntPacker.getRight(pair);

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(IntPacker.packInts(child, distances[child]));
                             });
        }

        List<Integer> path = new ArrayList<>();

        if (visited[goal] == -1) {
            path.add(-1);
            return path;
        }

        path.add(goal);
        int cur = goal;
        while (cur != start) {
            cur = visited[cur];
            path.add(cur);
        }

        Collections.reverse(path);
        return path;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class IntPacker {

        public static long packInts(int a, int b) {
            return (((long) a) << 32) | (b & 0xffffffffL);
        }

        public static int getLeft(long pair) {
            return (int)(pair >> 32);
        }

        public static int getRight(long pair) {
            return (int)pair;
        }

    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        int[] visited = dijkstra(graph, 1, n, n);
        if (visited[n] == -1) {
            System.out.println("-1");
            return;
        }

        int[] path = new int[Math.max(n, m)];
        int start = path.length - 1;
        int cur = n;
        path[start--] = n;
        while (cur != 1) {
            cur = visited[cur];
            path[start--] = cur;
        }
        System.out.print(path[start + 1]);
        for (int i = start + 2; i < path.length; i++) {
            System.out.print(" " + path[i]);
        }
    }

    public static int[] dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        int[] visited = new int[n + 1];
        Arrays.fill(visited, -1);

        if (!graph.containsKey(start)) {
            return visited;
        }

        int[] distances = new int[n + 1];
        Arrays.fill(distances, Integer.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Long> priorityQueue = new PriorityQueue<>((o1, o2) -> IntPacker.getRight(o1) - IntPacker.getRight(o2));
        priorityQueue.add(IntPacker.packInts(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            long pair = priorityQueue.remove();
            int vertex = IntPacker.getLeft(pair);
            int weight = IntPacker.getRight(pair);

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(IntPacker.packInts(child, distances[child]));
                             });
        }

        return visited;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class Pair<A, B> {

        final A left;
        final B right;

        public Pair(A left, B right) {
            this.left = left;
            this.right = right;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Pair<?, ?> pair = (Pair<?, ?>) o;
            return Objects.equals(left, pair.left) &&
                    Objects.equals(right, pair.right);
        }

        @Override
        public int hashCode() {
            return Objects.hash(left, right);
        }
    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        System.out.println(dijkstra(graph, 1, n, n).stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        List<Integer> path = new ArrayList<>();

        if (!graph.containsKey(start)) {
            path.add(-1);
            return path;
        }

        int[] visited = new int[n + 1];
        long[] distances = new long[n + 1];
        Arrays.fill(visited, -1);
        Arrays.fill(distances, Long.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Pair<Integer, Long>> priorityQueue = new PriorityQueue<>((o1, o2) -> {
            if (o1.right < o2.right) {
                return -1;
            } else if (o1.right.equals(o2.right)) {
                return 0;
            } else {
                return 1;
            }
        });
        priorityQueue.add(new Pair<>(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            Pair<Integer, Long> pair = priorityQueue.remove();
            int vertex = pair.left;
            long weight = pair.right;

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(new Pair<>(child, distances[child]));
                             });
        }

        if (visited[goal] == -1) {
            path.add(-1);
            return path;
        }

        path.add(goal);
        int cur = goal;
        while (cur != start) {
            cur = visited[cur];
            path.add(cur);
        }

        Collections.reverse(path);
        return path;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                graph.put(edgeDesc[0], new HashMap<>(edgeDesc[1], edgeDesc[2]));
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                graph.put(edgeDesc[1], new HashMap<>(edgeDesc[0], edgeDesc[2]));
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        PriorityQueue<SimpleEntry<Integer, Integer>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue() - o2.getValue());
        Map<Integer, Integer> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, 1));

        while (!priorityQueue.isEmpty()) {
            Integer vertex = priorityQueue.remove().getKey();
            if (vertex.equals(goal)) {
                break;
            }

            for (Integer child : graph.get(vertex).keySet()) {
                if (!visited.containsKey(child)) {
                    priorityQueue.add(new SimpleEntry<>(child, graph.get(vertex).get(child)));
                    visited.put(child, vertex);
                }
            }
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur);
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class IntPacker {

        public static long packInts(int a, int b) {
            return (((long) a) << 32) | (b & 0xffffffffL);
        }

        public static int getLeft(long pair) {
            return (int)(pair >> 32);
        }

        public static int getRight(long pair) {
            return (int)pair;
        }

    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        int[] visited = dijkstra(graph, 1, n, n);
        if (visited[n] == -1) {
            System.out.println("-1");
            return;
        }

        int[] path = new int[n];
        int start = path.length - 1;
        int cur = n;
        path[start--] = n;
        while (cur != 1) {
            cur = visited[cur];
            path[start--] = cur;
        }
        System.out.print(path[start + 1]);
        for (int i = start + 2; i < path.length; i++) {
            System.out.print(" " + path[i]);
        }
    }

    public static int[] dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        int[] visited = new int[n + 1];
        Arrays.fill(visited, -1);

        if (!graph.containsKey(start)) {
            return visited;
        }

        int[] distances = new int[n + 1];
        Arrays.fill(distances, Integer.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Long> priorityQueue = new PriorityQueue<>((o1, o2) -> IntPacker.getRight(o1) - IntPacker.getRight(o2));
        priorityQueue.add(IntPacker.packInts(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            long pair = priorityQueue.remove();
            int vertex = IntPacker.getLeft(pair);
            int weight = IntPacker.getRight(pair);

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(IntPacker.packInts(child, distances[child]));
                             });
        }

        return visited;
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        // If graph does not contain start return
        if (!graph.containsKey(start)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        }

        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, SimpleEntry<Integer, Integer>> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 0)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            if (!visited.containsKey(vertex) || visited.get(vertex).getValue() > entry.getValue().getValue()) {
                visited.put(vertex, new SimpleEntry<>(entry.getKey(), entry.getValue().getValue()));
            }
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child) + visited.get(vertex).getValue())));
            });
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur).getKey();
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, SimpleEntry<Integer, Integer>> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 1)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            if (!visited.containsKey(vertex) || visited.get(vertex).getValue() > entry.getValue().getValue()) {
                visited.put(vertex, new SimpleEntry<>(entry.getKey(), entry.getValue().getValue()));
            }
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child))));
            });
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur).getKey();
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        dijkstra(graph, 1, n);
    }

    public static void dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        // If graph does not contain start return
        if (!graph.containsKey(start)) {
            System.out.println("-1");
            return;
        }

        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, SimpleEntry<Integer, Integer>> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 0)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            if (!visited.containsKey(vertex) || visited.get(vertex).getValue() > entry.getValue().getValue()) {
                visited.put(vertex, new SimpleEntry<>(entry.getKey(), entry.getValue().getValue()));
            }
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child) + visited.get(vertex).getValue())));
            });
        }

        if (!visited.containsKey(goal)) {
            System.out.println("-1");
        } else {
            Integer cur = goal;
            StringBuilder sb = new StringBuilder();
            sb.append(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur).getKey();
                sb.insert(0, " ");
                sb.insert(0, cur);
            }
            System.out.println(sb);
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, Integer> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 1)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            visited.put(vertex, entry.getKey());
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child))));
            });
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur);
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        List<Integer> path = dijkstra(graph, 1, n);
        System.out.println(path.stream().map(Object::toString).collect(Collectors.joining(" ")));
    }

    public static List<Integer> dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal) {
        PriorityQueue<SimpleEntry<Integer, SimpleEntry<Integer, Integer>>> priorityQueue = new PriorityQueue<>((o1, o2) -> o1.getValue().getValue() - o2.getValue().getValue());
        Map<Integer, SimpleEntry<Integer, Integer>> visited = new HashMap<>();

        priorityQueue.add(new SimpleEntry<>(start, new SimpleEntry<>(start, 1)));

        while (!priorityQueue.isEmpty()) {
            SimpleEntry<Integer, SimpleEntry<Integer, Integer>> entry = priorityQueue.remove();
            Integer vertex = entry.getValue().getKey();
            if (!visited.containsKey(vertex) || visited.get(vertex).getValue() > entry.getValue().getValue() + visited.get(vertex).getValue()) {
                visited.put(vertex, new SimpleEntry<>(entry.getKey(), entry.getValue().getValue()));
            }
            if (vertex.equals(goal)) {
                break;
            }

            graph.get(vertex).keySet().stream().filter(child -> !visited.containsKey(child)).forEach(child -> {
                priorityQueue.add(new SimpleEntry<>(vertex, new SimpleEntry<>(child, graph.get(vertex).get(child))));
            });
        }

        if (!visited.containsKey(goal)) {
            List<Integer> list = new ArrayList<>();
            list.add(-1);
            return list;
        } else {
            Integer cur = goal;
            List<Integer> path = new ArrayList<>();
            path.add(goal);
            while (!cur.equals(start)) {
                cur = visited.get(cur).getKey();
                path.add(cur);
            }
            Collections.reverse(path);
            return path;
        }
    }

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.AbstractMap.SimpleEntry;
import java.util.*;
import java.util.stream.Collectors;

public class Dijkstra {

    public static class IntPacker {

        public static long packInts(int a, int b) {
            return (((long) a) << 32) | (b & 0xffffffffL);
        }

        public static int getLeft(long pair) {
            return (int)(pair >> 32);
        }

        public static int getRight(long pair) {
            return (int)pair;
        }

    }

    public static void main(String ... args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int[] nm = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();
        int n = nm[0];
        int m = nm[1];

        // Construct adjacency list graph
        Map<Integer, Map<Integer, Integer>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            int[] edgeDesc = Arrays.asList(reader.readLine().split(" ")).stream().mapToInt(Integer::parseInt).toArray();

            // Two edges since undirected
            if (!graph.containsKey(edgeDesc[0])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[1], edgeDesc[2]);
                graph.put(edgeDesc[0], edgeMap);
            } else {
                graph.get(edgeDesc[0]).put(edgeDesc[1], edgeDesc[2]);
            }

            if (!graph.containsKey(edgeDesc[1])) {
                Map<Integer, Integer> edgeMap = new HashMap<>();
                edgeMap.put(edgeDesc[0], edgeDesc[2]);
                graph.put(edgeDesc[1], edgeMap);
            } else {
                graph.get(edgeDesc[1]).put(edgeDesc[0], edgeDesc[2]);
            }
        }

        int[] visited = dijkstra(graph, 1, n, n);
        if (visited[n] == -1) {
            System.out.println("-1");
            return;
        }

        int[] path = new int[m];
        int start = path.length - 1;
        int cur = n;
        path[start--] = n;
        while (cur != 1) {
            cur = visited[cur];
            path[start--] = cur;
        }
        System.out.print(path[start + 1]);
        for (int i = start + 2; i < path.length; i++) {
            System.out.print(" " + path[i]);
        }
    }

    public static int[] dijkstra(Map<Integer, Map<Integer, Integer>> graph, Integer start, Integer goal, int n) {
        int[] visited = new int[n + 1];
        Arrays.fill(visited, -1);

        if (!graph.containsKey(start)) {
            return visited;
        }

        int[] distances = new int[n + 1];
        Arrays.fill(distances, Integer.MAX_VALUE);
        distances[0] = 0;

        PriorityQueue<Long> priorityQueue = new PriorityQueue<>((o1, o2) -> IntPacker.getRight(o1) - IntPacker.getRight(o2));
        priorityQueue.add(IntPacker.packInts(start, distances[0]));

        while (!priorityQueue.isEmpty()) {
            long pair = priorityQueue.remove();
            int vertex = IntPacker.getLeft(pair);
            int weight = IntPacker.getRight(pair);

            if (vertex == goal) {
                break;
            }

            graph.get(vertex).keySet()
                             .stream()
                             .filter(child -> distances[child] > weight + graph.get(vertex).get(child))
                             .forEach(child -> {
                                 distances[child] = weight + graph.get(vertex).get(child);
                                 visited[child] = vertex;
                                 priorityQueue.add(IntPacker.packInts(child, distances[child]));
                             });
        }

        return visited;
    }

}


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Scanner;

public class SemiDijkstra {
	public static int nextNode(HashSet<Integer> minHeap, int[] distance) {
		Iterator<Integer> itr = minHeap.iterator();
		int nextNode = itr.next().intValue();
		while (itr.hasNext()) {
			int i = itr.next().intValue();
			if (distance[nextNode-1] > distance[i-1]) {
				nextNode = i;
			}
		}
		return nextNode;
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int m = scan.nextInt();

		int[][] graph = new int[n][n];
		int[] distance = new int[n];

		HashSet<Integer> notVisited = new HashSet<>();

		// Fill with initial values.
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < n; j++) {
				if (i == j) {
					graph[i][j] = 0;
				}
				else {
					graph[i][j] = -1;
				}
			}
			distance[i] = Integer.MAX_VALUE;
			notVisited.add(new Integer(i + 1));
		}

		// Set up graph.
		while (scan.hasNextLine()) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			int w = scan.nextInt();

			graph[a-1][b-1] = w;
		}

		HashSet<Integer> minHeap = new HashSet<>();
		int[] sourceOfShortestDistance = new int[n];

		minHeap.add(new Integer(1));
		boolean shortestPathFound = false;

		while (!minHeap.isEmpty() && !notVisited.isEmpty()) {
			int nodeAsInt = nextNode(minHeap, distance);
			Integer node = new Integer(nodeAsInt);

			if (nodeAsInt == n) {
				shortestPathFound = true;
				int source = sourceOfShortestDistance[n-1];
				ArrayList<Integer> list = new ArrayList<>();
				list.add(n);
				while (source != 1) {
					list.add(new Integer(source));
					source = sourceOfShortestDistance[source-1];
				}
				list.add(1);
				Collections.reverse(list);
				for (Integer i : list) {
					System.out.print(i + " ");
				}
				break;
			}

			// Add node's children and update distance.
			for (int i = 0; i < n; i++) {
				if (graph[nodeAsInt - 1][i] != -1 && notVisited.contains(new Integer(i + 1))) {
					// Add to queue.
					minHeap.add(new Integer(i + 1));

					int thisDistance = distance[nodeAsInt - 1] + graph[nodeAsInt - 1][i];

					if (distance[i] > thisDistance) {
						distance[i] = thisDistance;
						sourceOfShortestDistance[i] = nodeAsInt;
					}
				}
			}
			minHeap.remove(node);
			notVisited.remove(node);
		}
		if (!shortestPathFound) {
			System.out.print(-1);
		}

		scan.close();
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class SemiDijkstra3 {
	static class Pair<F, S> {
		F first;
		S second;

		public Pair(F first, S second) {
			this.first = first;
			this.second = second;
		}

		public F getFirst() {
			return first;
		}

		public S getSecond() {
			return second;
		}
	}

	static class WeightComparator implements Comparator<Pair<Integer, Integer>> {
		public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
			Integer int1 = p1.getSecond();
			Integer int2 = p2.getSecond();

			return int1.compareTo(int2);
		}
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int m = scan.nextInt();

		// node -> set of chidren
		// first element in pair is the next node, second is the weight
		HashMap<Integer, HashSet<Pair<Integer, Integer>>> graph = new HashMap<>();
		int[] distance = new int[n];

		HashSet<Integer> notVisited = new HashSet<>();

		// Fill with initial values.
		for (int i = 0; i < n; i++) {
			distance[i] = Integer.MAX_VALUE;
			notVisited.add(new Integer(i + 1));
		}

		// Set up graph.
		while (scan.hasNextInt()) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			int w = scan.nextInt();

			Integer A = new Integer(a);
			Integer B = new Integer(b);
			Integer W = new Integer(w);

			HashSet<Pair<Integer, Integer>> set = graph.get(A);
			if (set == null) {
				set = new HashSet<Pair<Integer, Integer>>();
			}
			Pair<Integer, Integer> p = new Pair<>(B, W);
			set.add(p);
			graph.put(A, set);

			set = graph.get(B);
			if (set == null) {
				set = new HashSet<Pair<Integer, Integer>>();
			}
			p = new Pair<>(A, W);
			set.add(p);
			graph.put(B, set);
		}

		PriorityQueue<Pair<Integer, Integer>> queue = new PriorityQueue<>(1, new WeightComparator());
		queue.add(new Pair<Integer, Integer>(1, 0));
		distance[0] = 0;
		int[] sourceOfShortestDistance = new int[n];

		boolean shortestPathFound = false;

		while (!queue.isEmpty() && !notVisited.isEmpty()) {
			Pair<Integer, Integer> pair = queue.poll();
			Integer node = pair.getFirst();
			int nodeAsInt = node.intValue();

			if (nodeAsInt == n) {
				shortestPathFound = true;
				int source = sourceOfShortestDistance[n-1];
				ArrayList<Integer> list = new ArrayList<>();
				list.add(n);
				while (source != 1) {
					list.add(new Integer(source));
					source = sourceOfShortestDistance[source-1];
				}
				list.add(1);

				for (int i = list.length() - 1; i >= 0; i--) {
					System.out.print(i + " ");
				}
				// Collections.reverse(list);
				// for (Integer i : list) {
				// 	System.out.print(i + " ");
				// }
				break;
			}

			// Add node's children and update distance.
			if (graph.get(node) != null) {
				Iterator<Pair<Integer, Integer>> itr = graph.get(node).iterator();
				while (itr.hasNext()) {
					Pair<Integer, Integer> edge = itr.next();
				 	Integer i = edge.getFirst();
				 	Integer w = edge.getSecond();

				 	int nextNode = i.intValue();
				 	int wAsInt = w.intValue();

					if (notVisited.contains(nextNode)) {
						int thisDistance = distance[nodeAsInt - 1] + wAsInt;

						if (distance[nextNode - 1] > thisDistance) {
							distance[nextNode - 1] = thisDistance;
							sourceOfShortestDistance[nextNode - 1] = nodeAsInt;
						}

						// // Add to queue.
						queue.add(new Pair<Integer, Integer>(nextNode, distance[nextNode - 1]));
						inQueue.add(nextNode);
					}
				}
			}

			notVisited.remove(node);
		}
		if (!shortestPathFound) {
			System.out.print(-1);
		}

		scan.close();
	}
}


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;

public class SemiDijkstra3 {
	static class Pair<F, S> {
		F first;
		S second;

		public Pair(F first, S second) {
			this.first = first;
			this.second = second;
		}

		public F getFirst() {
			return first;
		}

		public S getSecond() {
			return second;
		}
	}

	static class WeightComparator implements Comparator<Pair<Integer, Integer>> {
		public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
			Integer int1 = p1.getSecond();
			Integer int2 = p2.getSecond();

			return int1.compareTo(int2);
		}
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		int m = scan.nextInt();

		// node -> set of chidren
		// first element in pair is the next node, second is the weight
		HashMap<Integer, HashSet<Pair<Integer, Integer>>> graph = new HashMap<>();
		int[] distance = new int[n];

		HashSet<Integer> notVisited = new HashSet<>();

		// Fill with initial values.
		for (int i = 0; i < n; i++) {
			distance[i] = Integer.MAX_VALUE;
			notVisited.add(new Integer(i + 1));
		}

		// Set up graph.
		while (scan.hasNextInt()) {
			int a = scan.nextInt();
			int b = scan.nextInt();
			int w = scan.nextInt();

			Integer A = new Integer(a);
			Integer B = new Integer(b);
			Integer W = new Integer(w);

			HashSet<Pair<Integer, Integer>> set = graph.get(A);
			if (set == null) {
				set = new HashSet<Pair<Integer, Integer>>();
			}
			Pair<Integer, Integer> p = new Pair<>(B, W);
			set.add(p);
			graph.put(A, set);

			set = graph.get(B);
			if (set == null) {
				set = new HashSet<Pair<Integer, Integer>>();
			}
			p = new Pair<>(A, W);
			set.add(p);
			graph.put(B, set);
		}

		PriorityQueue<Pair<Integer, Integer>> queue = new PriorityQueue<>(1, new WeightComparator());
		queue.add(new Pair<Integer, Integer>(1, 0));
		distance[0] = 0;
		int[] sourceOfShortestDistance = new int[n];

		boolean shortestPathFound = false;

		while (!queue.isEmpty() && !notVisited.isEmpty()) {
			Pair<Integer, Integer> pair = queue.poll();
			Integer node = pair.getFirst();
			int nodeAsInt = node.intValue();

			if (nodeAsInt == n) {
				shortestPathFound = true;
				int source = sourceOfShortestDistance[n-1];
				ArrayList<Integer> list = new ArrayList<>();
				list.add(n);
				while (source != 1) {
					list.add(new Integer(source));
					source = sourceOfShortestDistance[source-1];
				}
				list.add(1);

				for (int i = list.length() - 1; i >= 0; i--) {
					System.out.print(list.get(i) + " ");
				}
				// Collections.reverse(list);
				// for (Integer i : list) {
				// 	System.out.print(i + " ");
				// }
				break;
			}

			// Add node's children and update distance.
			if (graph.get(node) != null) {
				Iterator<Pair<Integer, Integer>> itr = graph.get(node).iterator();
				while (itr.hasNext()) {
					Pair<Integer, Integer> edge = itr.next();
				 	Integer i = edge.getFirst();
				 	Integer w = edge.getSecond();

				 	int nextNode = i.intValue();
				 	int wAsInt = w.intValue();

					if (notVisited.contains(nextNode)) {
						int thisDistance = distance[nodeAsInt - 1] + wAsInt;

						if (distance[nextNode - 1] > thisDistance) {
							distance[nextNode - 1] = thisDistance;
							sourceOfShortestDistance[nextNode - 1] = nodeAsInt;
						}

						// // Add to queue.
						queue.add(new Pair<Integer, Integer>(nextNode, distance[nextNode - 1]));
						inQueue.add(nextNode);
					}
				}
			}

			notVisited.remove(node);
		}
		if (!shortestPathFound) {
			System.out.print(-1);
		}

		scan.close();
	}
}