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

public class DPQ {
    public static class Node implements Comparable<Node> {
        int id;
        int prev;
        List<Edge> neighbors;
        long cost;
        int count;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.count = -1;
            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, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
            if (current.id == 0) {
                current.count = count;
            }
            /*
            if (nextNode == destination) {
                //System.out.println(current.id+1);
                int[] path = new int[destination.count+1];
                for (int i= 0; i<destination.count; i++) {
                    path[i] = nextNode.id+1;
                    nextNode = graph.get(nextNode.prev);
                }
                path[destination.count] = 1;
                return path;
            }
            */
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                //System.out.println((current.id+1)+ ":  " + (nextNode.id+1));
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    pq.remove(nextNode);
                    nextNode.cost = newCost;
                    //nextNode.visited = true;
                    pq.add(nextNode);
                    nextNode.count = current.count+1;
                    nextNode.prev = current.id;
                    //System.out.println((current.count)+ ":  " + (nextNode.id+1));
                }
            }
            current.visited = true;
        }
        //return null;
    }

    static class FastReader
    {
        java.io.BufferedReader br;
        java.util.StringTokenizer st;

        public FastReader()
        {
            br = new java.io.BufferedReader(new
                    java.io.InputStreamReader(System.in));
        }

        String next()
        {
            while (st == null || !st.hasMoreElements())
            {
                try
                {
                    st = new java.util.StringTokenizer(br.readLine());
                }
                catch (java.io.IOException  e)
                {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt()
        {
            return Integer.parseInt(next());
        }

        long nextLong()
        {
            return Long.parseLong(next());
        }

        double nextDouble()
        {
            return Double.parseDouble(next());
        }

        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (java.io.IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

    public static void main(String[] args) throws IOException {
        final FastReader scan = new FastReader();

        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();

            Node nodeStart = graph.get(start);
            Node nodeEnd = graph.get(end);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));

            nodeStart = graph.get(end);
            nodeEnd = graph.get(start);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

        // Convert to 0 indexing
        final int source = 0;
        final int sink = numVertices-1;

        // All of the costs will be saved in the node objects
        dijkstra(graph, graph.get(source), graph.get(sink));
        /*
        if (path == null) {
            System.out.print(-1);
        } else {
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length-1-i] + " ");
            }
        }
        */
        long ans = graph.get(sink).cost;
        if (ans == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            Node nextNode = graph.get(sink);
            int[] path = new int[graph.get(sink).count + 1];
            for (int i = 0; i < graph.get(sink).count; i++) {
                path[i] = nextNode.id + 1;
                nextNode = graph.get(nextNode.prev);
            }
            path[graph.get(sink).count] = 1;
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length - 1 - i] + " ");
            }
        }

    }
}


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

public class DPQ {
    public static class Node implements Comparable<Node> {
        int id;
        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;
        }

        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 int[] dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        int[] path = new int[graph.size()];
        Arrays.fill(path, 0);
        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
            if (path[count] == 0) {
                path[count] = current.id+1;
            }
            // System.out.println(current.id+1);
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                //System.out.println((current.id+1)+ ":  " + (nextNode.id+1));
                long newCost = current.cost + edge.weight;
                if (path[count] == 0) {
                    path[count] = current.id+1;
                }

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    path[count+1] = nextNode.id+1;
                    // System.out.println(count+":   " + (nextNode.id+1));
                }
                if (nextNode == destination) {
                    //System.out.println(current.id+1);
                    for (int i = 0; i < graph.size(); i++) {
                        if (path[i] == current.id+1) {
                            count = i;
                        }
                    }
                    for (int i = count+1; i < graph.size(); i++) {
                        if (i == count+1) {
                            path[i] = destination.id+1;
                        } else {
                            path[i] = 0;
                        }
                    }

                    return path;
                }
            }
            count++;
        }
        return null;
    }

    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();

            Node nodeStart = graph.get(start);
            Node nodeEnd = graph.get(end);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));

            nodeStart = graph.get(end);
            nodeEnd = graph.get(start);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

        // Convert to 0 indexing
        final int source = 0;
        final int sink = numVertices-1;

        // All of the costs will be saved in the node objects
        int[] path = dijkstra(graph, graph.get(source), graph.get(sink));

        if (path == null) {
            System.out.print(-1);
        } else {
            for (int i = 0; i < numVertices; i++) {
                if (path[i] != 0)
                    System.out.print(path[i] + " ");
                else
                    break;
            }
        }

    }
}


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

public class DPQ {
    public static class Node implements Comparable<Node> {
        int id;
        int prev;
        List<Edge> neighbors;
        long cost;
        int count;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.count = -1;
            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, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
            if (current.id == 0) {
                current.count = count;
            }
            /*
            if (nextNode == destination) {
                //System.out.println(current.id+1);
                int[] path = new int[destination.count+1];
                for (int i= 0; i<destination.count; i++) {
                    path[i] = nextNode.id+1;
                    nextNode = graph.get(nextNode.prev);
                }
                path[destination.count] = 1;
                return path;
            }
            */
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                //System.out.println((current.id+1)+ ":  " + (nextNode.id+1));
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    pq.remove(nextNode);
                    nextNode.cost = newCost;
                    //nextNode.visited = true;
                    pq.add(nextNode);
                    nextNode.count = current.count+1;
                    nextNode.prev = current.id;
                    //System.out.println((current.count)+ ":  " + (nextNode.id+1));
                }
            }
            current.visited = true;
        }
        //return null;
    }

    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();

            Node nodeStart = graph.get(start);
            Node nodeEnd = graph.get(end);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));

            nodeStart = graph.get(end);
            nodeEnd = graph.get(start);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

        // Convert to 0 indexing
        final int source = 0;
        final int sink = numVertices-1;

        // All of the costs will be saved in the node objects
        dijkstra(graph, graph.get(source), graph.get(sink));
        /*
        if (path == null) {
            System.out.print(-1);
        } else {
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length-1-i] + " ");
            }
        }
        */
        long ans = graph.get(sink).cost;
        if (ans == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            Node nextNode = graph.get(sink);
            //int[] path = new int[graph.get(sink).count + 1];
            final ArrayList path = new ArrayList();
            for (int i = 0; i < graph.get(sink).count; i++) {
                path.add(0, nextNode.id + 1);
                nextNode = graph.get(nextNode.prev);
            }
            //path[graph.get(sink).count] = 1;
            path.add(0, 1);
            System.out.print(path);
        }
    }
}


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

public class DPQ {
    public static class Node implements Comparable<Node> {
        int id;
        int prev;
        List<Edge> neighbors;
        long cost;
        int count;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.count = -1;
            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 int[] dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
            if (current.id == 0) {
                current.count = count;
            }
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                //System.out.println((current.id+1)+ ":  " + (nextNode.id+1));
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    pq.remove(nextNode);
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    nextNode.count = current.count+1;
                    nextNode.prev = current.id;
                    //System.out.println((current.count)+ ":  " + (nextNode.id+1));
                }
                if (nextNode == destination) {
                    //System.out.println(current.id+1);
                    int[] path = new int[destination.count+1];
                    for (int i= 0; i<destination.count; i++) {
                        path[i] = nextNode.id+1;
                        nextNode = graph.get(nextNode.prev);
                    }
                    path[destination.count] = 1;
                    return path;
                }
            }
        }
        return null;
    }

    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();

            Node nodeStart = graph.get(start);
            Node nodeEnd = graph.get(end);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));

            nodeStart = graph.get(end);
            nodeEnd = graph.get(start);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

        // Convert to 0 indexing
        final int source = 0;
        final int sink = numVertices-1;

        // All of the costs will be saved in the node objects
        int path[] = dijkstra(graph, graph.get(source), graph.get(sink));

        if (path == null) {
            System.out.print(-1);
        } else {
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length-1-i] + " ");
            }
        }

    }
}


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

public class DPQ {
    public static class Node implements Comparable<Node> {
        int id;
        int prev;
        List<Edge> neighbors;
        long cost;
        int count;
        boolean visited;

        public Node(int id) {
            this.id = id;
            this.neighbors = new ArrayList<>();
            this.cost = Long.MAX_VALUE;
            this.count = -1;
            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, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 0;
        while (!pq.isEmpty()) {
            Node current = pq.remove();
            if (current.id == 0) {
                current.count = count;
            }
            /*
            if (nextNode == destination) {
                //System.out.println(current.id+1);
                int[] path = new int[destination.count+1];
                for (int i= 0; i<destination.count; i++) {
                    path[i] = nextNode.id+1;
                    nextNode = graph.get(nextNode.prev);
                }
                path[destination.count] = 1;
                return path;
            }
            */
            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                //System.out.println((current.id+1)+ ":  " + (nextNode.id+1));
                long newCost = current.cost + edge.weight;

                if (newCost < nextNode.cost) {
                    pq.remove(nextNode);
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    nextNode.count = current.count+1;
                    nextNode.prev = current.id;
                    //System.out.println((current.count)+ ":  " + (nextNode.id+1));
                }
            }
        }
        //return null;
    }

    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();

            Node nodeStart = graph.get(start);
            Node nodeEnd = graph.get(end);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));

            nodeStart = graph.get(end);
            nodeEnd = graph.get(start);
            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

        // Convert to 0 indexing
        final int source = 0;
        final int sink = numVertices-1;

        // All of the costs will be saved in the node objects
        dijkstra(graph, graph.get(source), graph.get(sink));
        /*
        if (path == null) {
            System.out.print(-1);
        } else {
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length-1-i] + " ");
            }
        }
        */
        long ans = graph.get(sink).cost;
        if (ans == Long.MAX_VALUE) {
            System.out.println(-1);
        } else {
            Node nextNode = graph.get(sink);
            int[] path = new int[graph.get(sink).count + 1];
            for (int i = 0; i < graph.get(sink).count; i++) {
                path[i] = nextNode.id + 1;
                nextNode = graph.get(nextNode.prev);
            }
            path[graph.get(sink).count] = 1;
            for (int i = 0; i < path.length; i++) {
                System.out.print(path[path.length - 1 - i] + " ");
            }
        }

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;
                    p.offer(new Node1(vert, distances[vert]));
                }

            }
        }
        int num = v-1;
        while (parent[num] != num){
                num = parent[num];
        }
        if (parent[num] != 1){
            System.out.print(-1);
        }
        else{
            printPath(v-1);
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = i;
        }
        distances[src] = 0;

    }
    void printPath(int vert){
        if (parent[vert] != vert){
            printPath(parent[vert]);
            System.out.print(vert + " ");
        }
        else{
            System.out.print(1 + " ");
        }
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    int[] parent;
    int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        boolean[] visited = new boolean[v];
        initialize(src);
        p.offer(new Node1(src, 0));
        visited[0] = true;
        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w && !visited[vert]){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        int ans = d.distances[n];
        if (ans == Integer.MAX_VALUE)
        	System.out.print(-1);
        else{
        	d.printPath(n,1);
        }
        System.out.println();
    }
    void printPath(int vert, int src){
        int current = vert;
        int[] nodes = new int[v];
        nodes[1] = current;
        int size = 1;
        while (current != src){
        	size++;
        	current = parent[current];
        	nodes[size] = current;
        }
        for (int i = size; i >= 1; i--){
        	System.out.print(nodes[i] + " ");
        }
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        boolean check  = true;
        Stack<Integer> s = new Stack<>();
        if (parent[vert] == -1){
            System.out.print(-1);
        }
        else{
            while (check){
                s.push(vert);
                if (parent[vert] == -1){
                    break;
                }
                else{
                    vert = parent[vert];
                }
            }
            while (!s.empty()){
                System.out.print(s.pop() + " ");
            }
        }
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    int[] shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (u == this.v-1)
                return parent;
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;

                }
                p.offer(new Node1(vert, distances[vert]));
            }
        }
        return null;
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    static void printPath(int[] path, int vert){
        if (vert == -1){
            return;
        }
        printPath(path, path[vert]);

        System.out.print(vert + " ");
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        int[] path = d.shortest(1);
        if (path == null){
            System.out.print(-1);
        }
        else{
            printPath(path, n);
        }
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node(src, 0));
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }

    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;

        }
        distances[src] = 0;
        parent[src] = -1;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n);
        System.out.println();
    }
    void printPath(int vert){
        if (parent[vert] == -1){
            System.out.print((vert) + " ");
            return;
        }
        printPath(parent[vert]);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[u] + w < distances[vert]){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;

                }
                p.offer(new Node1(vert, distances[vert]));
            }
        }
        if (distances[v-1] == Integer.MAX_VALUE)
            System.out.print(-1);
        else{
            MyStack s = new MyStack(v);
            for (int a = v-1; a != 1; a = parent[a]){
                s.push(a);
            }
            System.out.print(1);
            while(!s.isEmpty()){
                System.out.print(" ");
                System.out.print(s.pop());
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = i;
        }
        distances[src] = 0;

    }
    void printPath(int vert){
        if (parent[vert] != vert){
            printPath(parent[vert]);
            System.out.print(vert + " ");
        }
        else{
            System.out.print(1 + " ");
        }
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}
class MyStack {
        int size;
        private int index;
        private int[] stackArray;

        public MyStack(int size) {
            this.size = size;
            this.index = -1;
            this.stackArray = new int[size];
        }

        public void push(int x) {
            index++;
            stackArray[index] = x;
        }

        public int peek() {
            return stackArray[index];
        }

        public int pop() {
            index--;
            return stackArray[index + 1];
        }

        public boolean isEmpty() {
            return index == -1;
        }

        public boolean isFull() {
            return index == size - 1;
        }

        public void clear() {
            index = -1;
        }

    }


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;

                }
                p.offer(new Node1(vert, distances[vert]));
            }
        }
        int num = v-1;
        while (parent[num] != num){
                num = parent[num];
        }
        if (parent[num] != 1){
            System.out.print(-1);
        }
        else{
            printPath(v-1);
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = i;
        }
        distances[src] = 0;

    }
    void printPath(int vert){
        if (parent[vert] != vert){
            printPath(parent[vert]);
            System.out.print(vert + " ");
        }
        else{
            System.out.print(1 + " ");
        }
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            int u = p.poll().getNode();
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        boolean check  = true;
        Stack<Integer> s = new Stack<>();
        if (parent[vert] == -1){
            System.out.print(-1);
        }
        else{
            while (check){
                s.push(vert);
                if (parent[vert] == -1){
                    break;
                }
                else{
                    vert = parent[vert];
                }
            }
            while (!s.empty()){
                System.out.print(s.pop() + " ");
            }
        }
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        if (parent[vert] == -1){
            System.out.print((vert == src) ? vert + " " : -1);
            return;
        }
        printPath(parent[vert], src);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            int u = p.poll().getNode();
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
        int num = v-1;
        if (distances[num] == Integer.MAX_VALUE){
            System.out.print(-1);
            return;
	}

        Stack <Integer> path = new Stack <>();
        while (num != 1){
                path.push(num);
                num = parent[num];
        }

        System.out.print(1 + " ");
        while (!path.isEmpty()){
                System.out.print(path.pop() + (path.isEmpty() ? "" : " "));
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;

                }
                p.offer(new Node1(vert, distances[vert]));
            }
        }
        if (distances[v-1] == Integer.MAX_VALUE)
            System.out.print(-1);
        else{
            Stack<Integer> s = new Stack<>();
            for (int a = v-1; a != 1; a = parent[a]){
                s.push(a);
            }
            System.out.print(1);
            while(!s.empty()){
                System.out.print(" ");
                System.out.print(s.pop());
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = i;
        }
        distances[src] = 0;

    }
    void printPath(int vert){
        if (parent[vert] != vert){
            printPath(parent[vert]);
            System.out.print(vert + " ");
        }
        else{
            System.out.print(1 + " ");
        }
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;

                }
                p.offer(new Node1(vert, distances[vert]));
            }
        }
        int num = v-1;
        if (distances[num] == Integer.MAX_VALUE){
            System.out.print(-1);
            return;
	}

        Stack <Integer> path = new Stack <>();
        while (num != 1){
                path.push(num);
                num = parent[num];
        }

        System.out.print(1 + " ");
        while (!path.isEmpty()){
                System.out.print(path.pop() + (path.isEmpty() ? "" : " "));
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        Arrays.fill(parent, -1);
        Arrays.fill(distances, Integer.MAX_VALUE);
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v];
        Arrays.fill(visited, false);
        visited[0] = true;
        distances[0] = 0;
        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;
                    visited[vert] = true;
                    p.offer(new Node1(vert, distances[vert]));
                }

            }
        }

        if (!visited[v-1])
            System.out.print(-1);
        else{
            printPath(v-1);
        }
    }

    void printPath(int vert){
        if (vert == -1){
            return;
        }
        printPath(parent[vert]);
        System.out.print(++vert + " ");
    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v-1, e-1, w-1);
        }
        d.shortest(0);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    int[] parent;
    int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        boolean[] visited = new boolean[v];
        initialize(src);
        p.offer(new Node1(src, 0));
        visited[0] = true;
        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w && !visited[vert]){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int n = input.nextInt(), m = input.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = input.nextInt();
            int e = input.nextInt();
            int w = input.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        int ans = d.distances[n];
        if (ans == Integer.MAX_VALUE)
        	System.out.print(-1);
        else{
        	d.printPath(n,1);
        }
        System.out.println();
    }
    void printPath(int vert, int src){
        int current = vert;
        int[] nodes = new int[v];
        nodes[1] = current;
        int size = 1;
        while (current != src){
        	size++;
        	current = parent[current];
        	nodes[size] = current;
        }
        for (int i = size; i >= 1; i--){
        	System.out.print(nodes[i] + " ");
        }
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        boolean[] visited = new boolean[v];
        initialize(src);
        p.offer(new Node1(src, 0));
        visited[0] = true;
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w && !visited[vert]){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        if (parent[vert] == -1){
            System.out.print((vert == src) ? vert + " " : -1);
            return;
        }
        printPath(parent[vert], src);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }

    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;

        }
        distances[src] = 0;
        parent[src] = -1;
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n);
        System.out.println();
    }
    void printPath(int vert){
        if (parent[vert] == -1){
            System.out.print((vert) + " ");
            return;
        }
        printPath(parent[vert]);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        boolean[] visited = new boolean[v];
        initialize(src);
        p.offer(new Node1(src, 0));
        visited[0] = true;
        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w && !visited[vert]){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        if (parent[vert] == -1){
            System.out.print((vert == src) ? vert + " " : -1);
            return;
        }
        printPath(parent[vert], src);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            int u = p.poll().getNode();
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n,1);
        System.out.println();
    }
    void printPath(int vert, int src){
        boolean check  = true;
        Stack<Integer> s = new Stack<>();
        if (parent[vert] == -1){
            System.out.print(-1);
            return;
        }
        while (vert != -1){
            s.push(vert);
            vert = parent[vert];
        }
        while (!s.empty()){
            System.out.print(s.pop() + (s.empty() ? " " : " "));
        }
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.util.*;

public class Main {
	// used for the nodes in graph
	public static class Node implements Comparable<Node> {
		int id;
		ArrayList<Edge> edges;
		boolean visited;
		int cost;
		Node parent;

		public Node(int id) {
			this.id = id;
			this.edges = new ArrayList<>();
			this.visited = false;
			this.cost = Integer.MAX_VALUE;
			this.parent = null;
		}

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

	// used for edges in the graph
	public static class Edge {
		int dest;
		int weight;

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

	// dijkstra algorithm to find shortest cost to a node
	public static void dijkstra(ArrayList<Node> graph, Node source) {
		PriorityQueue<Node> pq = new PriorityQueue<>();
		source.cost = 0;
		source.visited = true;
		pq.add(source);

		while (!pq.isEmpty()) {
			Node current = pq.remove();
			current.visited = true;

			for (Edge edge: current.edges) {
				Node dest = graph.get(edge.dest);
				int newCost = current.cost + edge.weight;

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

	// print shortest path
	public static void printPath(ArrayList<Node> graph, int source, int sink) {
		int current = sink;
		int[] nodesInPath = new int[graph.size()];
		nodesInPath[0] = current;

		int arraySize = 0;
		while (current != source) {
			arraySize++;
			current = graph.get(current).parent.id;
			nodesInPath[arraySize] = current;
		}

		for (int idx = arraySize; idx >= 0; idx--) {
			// convert to 1 base indexing
			int id = nodesInPath[idx] + 1;
			// print id of current node
			System.out.print(id + " ");
		}
	}

	// recursive version of printPath
	public static void printPathRec(ArrayList<Node> graph, Node current) {
		Node source = graph.get(0);

		if (current != source) {
			// recursively call this method
			printPathRec(graph, current.parent);
		}

		// convert to 1 base indexing
		int index = current.id + 1;
		// print id of current node
		System.out.print(index + " ");
	}

	// get inputs and runs program
	public static void inputAndRun() {
		final Scanner s = new Scanner(System.in);

		// scan first two ints
		final int numVertices = s.nextInt();
		final int numEdges = s.nextInt();

		// create graph as an array list and add nodes to list
		final ArrayList<Node> graph = new ArrayList<>();
		for (int i = 0; i < numVertices; i++) {
			graph.add(new Node(i));
		}

		// loop through given edges and connect nodes
		for (int i = 0; i < numEdges; i++) {
			final int id1 = s.nextInt() - 1;
			final int id2 = s.nextInt() - 1;
			final int weight = s.nextInt();

			final Node end1 = graph.get(id1);
			final Node end2 = graph.get(id2);

			end1.edges.add(new Edge(end2.id, weight));
			end2.edges.add(new Edge(end1.id, weight));
		}

		// source and sink nodes
		final int source = 0;
		final int sink = numVertices - 1;

		// run dijkstra
		dijkstra(graph, graph.get(0));

		// determine if path exists between source and sink,
		// if yes, then print path, else return -1
		int ans = graph.get(sink).cost;
		if (ans == Integer.MAX_VALUE) {
			System.out.print("-1");
		} else {
			printPath(graph, source, sink);
		}

		// close scanner
		s.close();

		/*
		 * print out for debugging graph
		 */
		// for (int i = 0; i < numVertices; i++) {
		// 	Node checkNode = graph.get(i);
		// 	System.out.print("ID: " + checkNode.id + ", ");
		// 	System.out.print("VISITED: " + checkNode.visited + ", ");
		// 	System.out.println("COST: " + checkNode.cost);
		// 	for (Edge edge: checkNode.edges) {
		// 		System.out.println("  edge to: " + graph.get(edge.dest).id + ", weight: " + edge.weight);
		// 	}
		// }
	}

	public static void main(String[] args) {
		inputAndRun();
	}
}


import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        while (!p.isEmpty()){
            int u = p.poll().getNode();
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    p.offer(new Node1(vert, distances[vert]));
                    parent[vert] = u;
                }
            }
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), m = in.nextInt();
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            int v = in.nextInt();
            int e = in.nextInt();
            int w = in.nextInt();
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        d.printPath(n);
        System.out.println();
    }
    void printPath(int vert){
        if (parent[vert] == -1){
            System.out.print((vert) + " ");
            return;
        }
        printPath(parent[vert]);

        System.out.print((vert) + " ");
    }
}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class dijkstra {
    private int v;
    private int[] parent;
    private int[] distances;
    private ArrayList<Node1>[] adj;
    dijkstra(int v){
        this.v = v;
        parent = new int[v];
        distances = new int[v];
        adj = new ArrayList[v];
        for (int i = 0; i < v; i++){
            adj[i] = new ArrayList<>();
        }
    }
    void addEdge(int u, int v, int w){
        adj[u].add(new Node1(v, w));
        adj[v].add(new Node1(u, w));
    }
    void shortest(int src){
        PriorityQueue<Node1> p = new PriorityQueue<>();
        initialize(src);
        p.offer(new Node1(src, 0));
        boolean[] visited = new boolean[this.v+1];

        while (!p.isEmpty()){
            Node1 node = p.poll();
            int u = node.getNode();
            int weight = node.getWeight();
            if (weight > distances[u])
                continue;
            if (visited[u])
                continue;
            visited[u] = true;
            Iterator<Node1> it = adj[u].iterator();
            while (it.hasNext()){
                Node1 n = it.next();
                int vert = n.getNode();
                int w = n.getWeight();
                if (distances[vert] > distances[u] + w){
                    distances[vert] = distances[u] + w;
                    parent[vert] = u;
                    p.offer(new Node1(vert, distances[vert]));
                }
            }
        }
        int num = v-1;
        if (distances[num] == Integer.MAX_VALUE){
            System.out.print(-1);
            return;
	}

        Stack <Integer> path = new Stack <>();
        while (num != 1){
                path.push(num);
                num = parent[num];
        }

        System.out.print(1 + " ");
        while (!path.isEmpty()){
                System.out.print(path.pop() + (path.isEmpty() ? "" : " "));
        }
    }
    void initialize(int src){
        for (int i = 1; i < this.v; i++){
            distances[i] = Integer.MAX_VALUE;
            parent[i] = -1;
        }
        distances[src] = 0;

    }
    public static void main(String[] args) throws IOException{
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

	StringTokenizer token = new StringTokenizer(read.readLine());
        int n = Integer.parseInt(token.nextToken()), m = Integer.parseInt(token.nextToken());
        dijkstra d = new dijkstra(n+1);
        for (int i = 0; i < m; i++){
            token = new StringTokenizer(read.readLine());
            int v = Integer.parseInt(token.nextToken());
            int e = Integer.parseInt(token.nextToken());
            int w = Integer.parseInt(token.nextToken());
            d.addEdge(v, e, w);
        }
        d.shortest(1);
        //d.printPath(n,1);
        System.out.println();
    }

}
class Node1 implements Comparable<Node1>{
    private int node;
    private int weight;
    Node1(int i, int j){
        node = i;
        weight = j;
    }

    public int getNode() {
        return node;
    }

    public int getWeight() {
        return weight;
    }

    public void setNode(int node) {
        this.node = node;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    @Override
    public int compareTo(Node1 node){
        if (this.weight < node.weight)
            return -1;
        else if (this.weight > node.weight)
            return 1;
        else
            return 0;
    }
}


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;
        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;
        }

        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);

        while (!pq.isEmpty()) {
            Node current = pq.remove();
			current.visited = true;
            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 {
                System.out.println(ans);
            }

    }
}


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<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();
            }

    }
}