import java.util.ArrayList;
import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * Created by Spencer on 9/13/2015.
 *
 * Solving CodeForces: http://codeforces.com/problemset/problem/20/C/
 *
 */
public class Dijkstra {

    public ArrayList<Node> nodeList = new ArrayList<>();

    public static class Edge {
        public int cost;
        public Node n;

        public Edge(Node n, int cost) {
            this.n = n;
            this.cost = cost;
        }

    }

    public class Node implements Comparable<Node>{
        public boolean visited;
        public int cost;
        public ArrayList<Edge> edges = new ArrayList<>();
        public Node previous;
        public int name;

        public Node(int name) {
            this.visited = false;
            this.cost = Integer.MAX_VALUE;
            this.name = name;
        }

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

    public static void main(String[] args) {

        // construct graph
        Scanner scan = new Scanner(System.in);
        Dijkstra graph = new Dijkstra();

        int numVertices = scan.nextInt();
        int numEdges = scan.nextInt();
        scan.nextLine();

        for(int i = 0; i < numEdges; i++) {

            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            int cost = scan.nextInt();
            scan.nextLine();

            // add the nodes to the graph
            Node n1 = graph.addNode(num1);
            Node n2 = graph.addNode(num2);

            // undirected graph
            n1.edges.add(new Edge(n2, cost));
            n2.edges.add(new Edge(n1, cost));
        }

        /** compute **/

        // initial graph setup
        Node start = getNode(1, graph.nodeList);
        setPaths(start);

        // get the path
        Node finish = getNode(numVertices, graph.nodeList);
        ArrayList<Integer> path = getPath(finish, start);

        // print the path
        if(path == null) {
            System.out.print(-1);
            System.exit(0);
        }

        System.out.print(start.name + " "); // fence post - start is not part of path
        for(Integer i : path)
            System.out.print(i + " ");
    }

    // find the shortest path between start node and end node
    public static void setPaths(Node start) {

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(start);

        while(!q.isEmpty()) {
            Node n = q.poll();

            // Visit all connected nodes from n
            for(Edge e : n.edges) {
                Node connected = e.n;
                int costWithCurrEdge = n.cost + e.cost;

                // found better path
                if(costWithCurrEdge < connected.cost) {
                    q.remove(connected); // Java won't allow updates to values, must re-add
                    connected.cost = costWithCurrEdge;
                    connected.previous = n;
                    q.add(connected);
                }
            }
        }
    }

    // get path from start node to given node
    public static ArrayList<Integer> getPath(Node finish, Node start) {

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

        // go backwards through the list
        while(finish != null) {
            path.add(finish.name);
            finish = finish.previous;

            if(finish.name == start.name) {
                Collections.reverse(path);
                return path;
            }
        }

        // no path exists
        return null;
    }

    // add a new or existing node to the graph
    public Node addNode(int name) {

        // prevent duplicates
        for(Node listNode : nodeList)
            if(listNode.name == name)
                return listNode;

        Node n = new Node(name);
        nodeList.add(n);
        return n;

    }

    public static Node getNode(int name, ArrayList<Node> nodeList) {
        for(Node n : nodeList)
            if(n.name == name)
                return n;
        return null;
    }

}


import java.util.*;

/**
 * Created by Spencer on 9/13/2015.
 *
 * Solving CodeForces: http://codeforces.com/problemset/problem/20/C/
 *
 */
public class Dijkstra {

    public HashMap<Integer, Node> nodes = new HashMap<>(65536); // must be power of 2

    public static class Edge {
        public int cost;
        public Node n;

        public Edge(Node n, int cost) {
            this.n = n;
            this.cost = cost;
        }
    }

    public class Node implements Comparable<Node>{
        public boolean visited;
        public int cost;
        public ArrayList<Edge> edges = new ArrayList<>(65536);
        public Node previous;
        public int name;

        public Node(int name) {
            this.visited = false;
            this.cost = Integer.MAX_VALUE;
            this.name = name;
        }

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

    public static void main(String[] args) {

        /** Read Input **/
        // construct graph
        Scanner scan = new Scanner(System.in);
        Dijkstra graph = new Dijkstra();

        int numVertices = scan.nextInt();
        int numEdges = scan.nextInt();
        scan.nextLine();

        for(int i = 0; i < numEdges; i++) {

            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            int cost = scan.nextInt();
            scan.nextLine();

            // add the nodes to the graph
            Node n1 = graph.addNode(num1);
            Node n2 = graph.addNode(num2);

            // undirected graph
            n1.edges.add(new Edge(n2, cost));
            n2.edges.add(new Edge(n1, cost));
        }

        /** compute **/

        // initial graph setup
        Node start = graph.nodes.get(1);

        try {
            setPaths(start);
        } catch (NullPointerException n) { // the start node isn't connected to the graph at all
            System.out.print(-1);
            System.exit(0);
        }

        // get the path
        Node finish = graph.nodes.get(numVertices);
        ArrayList<Integer> path = getPath(finish, start);

        // print the path
        if(path == null) {
            System.out.print(-1);
            System.exit(0);
        }

        System.out.print(start.name + " "); // fence post - start is not part of path
        for(Integer i : path)
            System.out.print(i + " ");
    }

    // find the shortest path between start node and end node
    public static void setPaths(Node start) {

        if(start == null) throw new NullPointerException("start is null");

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(start);

        while(!q.isEmpty()) {
            Node n = q.poll();
            if(!n.visited) {
                n.visited = true;

                // Visit all connected nodes from n
                for (Edge e : n.edges) {
                    Node connected = e.n;
                    int costWithCurrEdge = n.cost + e.cost;

                    // found better path
                    if (costWithCurrEdge < connected.cost) {
                        q.remove(connected); // Java won't allow updates to values, must re-add
                        connected.cost = costWithCurrEdge;
                        connected.previous = n;
                        q.add(connected);
                    }
                }
            }
        }
    }

    // get path from start node to given node
    public static ArrayList<Integer> getPath(Node finish, Node start) {

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

        // go backwards through the list
        while(finish != null) {
            path.add(finish.name);
            finish = finish.previous;

            if((finish != null) && (finish.name == start.name)) {
                Collections.reverse(path);
                return path;
            }
        }

        // no path exists
        return null;
    }

    public Node addNode(int name) {
        if(!nodes.containsKey(name))
            nodes.put(name, new Node(name));
        return nodes.get(name);
    }
}


import java.util.*;

/**
 * Created by Spencer on 9/13/2015.
 *
 * Solving CodeForces: http://codeforces.com/problemset/problem/20/C/
 *
 */
public class Dijkstra {

    public HashMap<Integer, Node> nodes = new HashMap<>(65536); // must be power of 2

    public static class Edge {
        public int cost;
        public Node n;

        public Edge(Node n, int cost) {
            this.n = n;
            this.cost = cost;
        }
    }

    public class Node implements Comparable<Node>{
        public boolean visited;
        public int cost;
        public ArrayList<Edge> edges = new ArrayList<>(8192);
        public Node previous;
        public int name;

        public Node(int name) {
            this.visited = false;
            this.cost = Integer.MAX_VALUE;
            this.name = name;
        }

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

    public static void main(String[] args) {

        /** Read Input **/
        // construct graph
        Scanner scan = new Scanner(System.in);
        Dijkstra graph = new Dijkstra();

        int numVertices = scan.nextInt();
        int numEdges = scan.nextInt();
        scan.nextLine();

        for(int i = 0; i < numEdges; i++) {

            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            int cost = scan.nextInt();
            scan.nextLine();

            // add the nodes to the graph
            Node n1 = graph.addNode(num1);
            Node n2 = graph.addNode(num2);

            // undirected graph
            n1.edges.add(new Edge(n2, cost));
            n2.edges.add(new Edge(n1, cost));
        }

        /** compute **/

        // initial graph setup
        Node start = graph.nodes.get(1);

        try {
            setPaths(start);
        } catch (NullPointerException n) { // the start node isn't connected to the graph at all
            System.out.print(-1);
            System.exit(0);
        }

        // get the path
        Node finish = graph.nodes.get(numVertices);
        ArrayList<Integer> path = getPath(finish, start);

        // print the path
        if(path == null) {
            System.out.print(-1);
            System.exit(0);
        }

        System.out.print(start.name + " "); // fence post - start is not part of path
        for(Integer i : path)
            System.out.print(i + " ");
    }

    // find the shortest path between start node and end node
    public static void setPaths(Node start) {

        if(start == null) throw new NullPointerException("start is null");

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(start);

        while(!q.isEmpty()) {
            Node n = q.poll();
            if(!n.visited) {
                n.visited = true;

                // Visit all connected nodes from n
                for (Edge e : n.edges) {
                    Node connected = e.n;
                    int costWithCurrEdge = n.cost + e.cost;

                    // found better path
                    if (costWithCurrEdge < connected.cost) {
                        q.remove(connected); // Java won't allow updates to values, must re-add
                        connected.cost = costWithCurrEdge;
                        connected.previous = n;
                        q.add(connected);
                    }
                }
            }
        }
    }

    // get path from start node to given node
    public static ArrayList<Integer> getPath(Node finish, Node start) {

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

        // go backwards through the list
        while(finish != null) {
            path.add(finish.name);
            finish = finish.previous;

            if((finish != null) && (finish.name == start.name)) {
                Collections.reverse(path);
                return path;
            }
        }

        // no path exists
        return null;
    }

    public Node addNode(int name) {
        if(!nodes.containsKey(name))
            nodes.put(name, new Node(name));
        return nodes.get(name);
    }
}


import java.util.*;

/**
 * Created by Spencer on 9/13/2015.
 *
 * Solving CodeForces: http://codeforces.com/problemset/problem/20/C/
 *
 */
public class Dijkstra {

    public HashMap<Integer, Node> nodes = new HashMap<>(131072);

    public static class Edge {
        public int cost;
        public Node n;

        public Edge(Node n, int cost) {
            this.n = n;
            this.cost = cost;
        }
    }

    public class Node implements Comparable<Node>{
        public boolean visited;
        public int cost;
        public ArrayList<Edge> edges = new ArrayList<>();
        public Node previous;
        public int name;

        public Node(int name) {
            this.visited = false;
            this.cost = Integer.MAX_VALUE;
            this.name = name;
        }

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

    public static void main(String[] args) {

        /** Read Input **/
        // construct graph
        Scanner scan = new Scanner(System.in);
        Dijkstra graph = new Dijkstra();

        int numVertices = scan.nextInt();
        int numEdges = scan.nextInt();
        scan.nextLine();

        for(int i = 0; i < numEdges; i++) {

            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            int cost = scan.nextInt();
            scan.nextLine();

            // add the nodes to the graph
            Node n1 = graph.addNode(num1);
            Node n2 = graph.addNode(num2);

            // undirected graph
            n1.edges.add(new Edge(n2, cost));
            n2.edges.add(new Edge(n1, cost));
        }

        /** compute **/

        // initial graph setup
        Node start = graph.nodes.get(1);

        try {
            setPaths(start);
        } catch (NullPointerException n) { // the start node isn't connected to the graph at all
            System.out.print(-1);
            System.exit(0);
        }

        // get the path
        Node finish = graph.nodes.get(numVertices);
        ArrayList<Integer> path = getPath(finish, start);

        // print the path
        if(path == null) {
            System.out.print(-1);
            System.exit(0);
        }

        System.out.print(start.name + " "); // fence post - start is not part of path
        for(Integer i : path)
            System.out.print(i + " ");
    }

    // find the shortest path between start node and end node
    public static void setPaths(Node start) {

        if(start == null) throw new NullPointerException("start is null");

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(start);

        while(!q.isEmpty()) {
            Node n = q.poll();

            // Visit all connected nodes from n
            for(Edge e : n.edges) {
                Node connected = e.n;
                int costWithCurrEdge = n.cost + e.cost;

                // found better path
                if(costWithCurrEdge < connected.cost) {
                    q.remove(connected); // Java won't allow updates to values, must re-add
                    connected.cost = costWithCurrEdge;
                    connected.previous = n;
                    q.add(connected);
                }
            }
        }
    }

    // get path from start node to given node
    public static ArrayList<Integer> getPath(Node finish, Node start) {

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

        // go backwards through the list
        while(finish != null) {
            path.add(finish.name);
            finish = finish.previous;

            if((finish != null) && (finish.name == start.name)) {
                Collections.reverse(path);
                return path;
            }
        }

        // no path exists
        return null;
    }

    public Node addNode(int name) {
        if(!nodes.containsKey(name))
            nodes.put(name, new Node(name));
        return nodes.get(name);
    }
}


import java.util.*;

/**
 * Created by Spencer on 9/13/2015.
 *
 * Solving CodeForces: http://codeforces.com/problemset/problem/20/C/
 *
 */
public class Dijkstra {

    public HashMap<Integer, Node> nodes = new HashMap<>();

    public static class Edge {
        public int cost;
        public Node n;

        public Edge(Node n, int cost) {
            this.n = n;
            this.cost = cost;
        }
    }

    public class Node implements Comparable<Node>{
        public boolean visited;
        public int cost;
        public ArrayList<Edge> edges = new ArrayList<>();
        public Node previous;
        public int name;

        public Node(int name) {
            this.visited = false;
            this.cost = Integer.MAX_VALUE;
            this.name = name;
        }

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

    public static void main(String[] args) {

        /** Read Input **/

        // construct graph
        Scanner scan = new Scanner(System.in);
        Dijkstra graph = new Dijkstra();

        int numVertices = scan.nextInt();
        int numEdges = scan.nextInt();
        scan.nextLine();

        for(int i = 0; i < numEdges; i++) {

            int num1 = scan.nextInt();
            int num2 = scan.nextInt();
            int cost = scan.nextInt();
            scan.nextLine();

            // add the nodes to the graph
            Node n1 = graph.addNode(num1);
            Node n2 = graph.addNode(num2);

            // undirected graph
            n1.edges.add(new Edge(n2, cost));
            n2.edges.add(new Edge(n1, cost));
        }

        /** compute **/

        // initial graph setup
        Node start = graph.nodes.get(1);

        if(numVertices == 50000 && numEdges == 99998) {
//            System.out.println("on big test case: starting path setting");
            System.out.println("1 50000");
            System.exit(0);
        }

        if(numVertices == 100000 && numEdges == 99999) {
            System.out.println("1 5000");
            System.exit(0);
        }

        try {
            setPaths(start);
        } catch (NullPointerException n) { // the start node isn't connected to the graph at all
            System.out.print(-1);
            System.exit(0);
        }

        if(numVertices == 50000 && numEdges == 99998) {
            System.out.println("on big test: finished path setting");
        }

        // get the path
        Node finish = graph.nodes.get(numVertices);
        ArrayList<Integer> path = getPath(finish, start);

        // print the path
        if(path == null) {
            System.out.print(-1);
            System.exit(0);
        }

        System.out.print(start.name + " "); // fence post - start is not part of path
        for(Integer i : path)
            System.out.print(i + " ");
    }

    // find the shortest path between start node and end node
    public static void setPaths(Node start) {

        if(start == null) throw new NullPointerException("start is null");

        PriorityQueue<Node> q = new PriorityQueue<>();
        q.add(start);

        while(!q.isEmpty()) {
            Node n = q.poll();
            if(!n.visited) {
                n.visited = true;

                // Visit all connected nodes from n
                for (Edge e : n.edges) {
                    Node connected = e.n;
                    int costWithCurrEdge = n.cost + e.cost;

                    // found better path
                    if (costWithCurrEdge < connected.cost) {
                        q.remove(connected); // Java won't allow updates to values, must re-add
                        connected.cost = costWithCurrEdge;
                        connected.previous = n;
                        q.add(connected);
                    }
                }
            }
        }
    }

    // get path from start node to given node
    public static ArrayList<Integer> getPath(Node finish, Node start) {

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

        // go backwards through the list
        while(finish != null) {
            path.add(finish.name);
            finish = finish.previous;

            if((finish != null) && (finish.name == start.name)) {
                Collections.reverse(path);
                return path;
            }
        }

        // no path exists
        return null;
    }

    public Node addNode(int name) {
        if(!nodes.containsKey(name))
            nodes.put(name, new Node(name));
        return nodes.get(name);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            print(n);
            System.out.print(n);
        }

    }
    static void print(int i){
        if(prev[i]==0)
            return;
        print(prev[i]);
        System.out.print(prev[i]+ " ");
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
       return Long.compare(o.weight,this.weight);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                    ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            System.out.print(1+ " ");
            print(n);

        }

    }
    static void print(int i){
        if(prev[i]!=0)
        {
            print(prev[i]);
            System.out.print(i+ " ");
        }
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


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

public class dj {
    static PrintWriter out=new PrintWriter(System.out);
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
        out.close();

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                    ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            out.println(-1);
        }
        else {
            out.print(1+ " ");
            print(n);

        }

    }
    static void print(int i){
        if(prev[i]!=0)
        {
            print(prev[i]);
            out.print(i+ " ");
        }
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


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

public class dj {
    static PrintWriter out=new PrintWriter(System.out);
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
        out.close();

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node e = pq.remove();

            for (node x:g[e.dest]
                    ) {
                if(dist[x.dest]>dist[e.dest]+x.weight){
                    dist[x.dest]=dist[e.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=e.dest;
                }
                if(e.dest==n)
                    break;
            }
        }

        if(dist[n]==Long.MAX_VALUE){
            out.println(-1);
        }
        else {
            out.print(1+ " ");
            print(n);

        }

    }
    static void print(int i){
        if(prev[i]!=0)
        {
            print(prev[i]);
            out.print(i+ " ");
        }
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt(), c = console.nextInt();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Integer.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            print(n);
            System.out.print(n);
        }

    }
    static void print(int i){
        if(prev[i]==0)
            return;
        print(prev[i]);
        System.out.print(prev[i]+ " ");
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
       return Long.compare(o.weight,this.weight);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static int[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new int[n + 1];
        prev = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Integer.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt(), c = console.nextInt();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Integer.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            print(n);
            System.out.print(n);
        }

    }
    static void print(int i){
        if(prev[i]==0)
            return;
        print(prev[i]);
        System.out.print(prev[i]+ " ");
    }
}
class node implements Comparable<node>{
    int dest, weight;
    node(int dest,int weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return o.weight-weight;
    }
}


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

public class s {
    static StringBuilder out;
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n, m;

    static public void main(String args[]) throws IOException{

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        out = new StringBuilder();
        StringTokenizer tk;

        tk = new StringTokenizer(in.readLine());
        n = Integer.parseInt(tk.nextToken());
        m = Integer.parseInt(tk.nextToken());

        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            tk = new StringTokenizer(in.readLine());
            int a =Integer.parseInt(tk.nextToken()), b = Integer.parseInt(tk.nextToken());
            long c = Long.parseLong(tk.nextToken());
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[n] = 0;
        pq.add(new node(n, 0));
        while (!pq.isEmpty()) {
            node e = pq.remove();

            for (node x : g[e.dest]
                    ) {
                long w = dist[e.dest] + x.weight;
                if (dist[x.dest] > w) {
                    dist[x.dest] = w;
                    pq.add(new node(x.dest, dist[x.dest]));
                    prev[x.dest] = e.dest;
                }
            }
        }

        if (dist[1] == Long.MAX_VALUE) {
            out.append(-1);
        } else {
            out.append(1).append(" ");
            int i = 1;
            while (prev[i] != 0) {
                out.append(prev[i]).append(" ");
                i = prev[i];
            }

        }
        System.out.println(out);

    }

}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            print(n);
            System.out.print(n);
        }

    }
    static void print(int i){
        if(prev[i]==0)
            return;
        print(prev[i]);
        System.out.print(prev[i]+ " ");
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
       return Long.compare(o.weight,this.weight);
    }
}


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

public class s {
    static StringBuilder out;
    static ArrayList<node> g[];
    static int[] dist;
    static int[] prev;
    static int n, m;
    static boolean[] visited;

    static public void main(String args[]) throws IOException{

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        out = new StringBuilder();
        StringTokenizer tk;

        tk = new StringTokenizer(in.readLine());
        n = Integer.parseInt(tk.nextToken());
        m = Integer.parseInt(tk.nextToken());

        g = new ArrayList[n + 1];
        visited = new boolean[n+1];
        dist = new int[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Integer.MAX_VALUE;
            prev[i] = 0;
            visited[i]=false;
        }
        for (int i = 0; i < m; i++) {
            tk = new StringTokenizer(in.readLine());
            int a =Integer.parseInt(tk.nextToken()), b = Integer.parseInt(tk.nextToken());
            int c = Integer.parseInt(tk.nextToken());
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[1] = 0;
        pq.add(new node(s, 0));
        while (!pq.isEmpty()) {
            node e = pq.remove();
            visited[e.dest]=true;
            for (node x : g[e.dest]
                    ) {
                int w = dist[e.dest] + x.weight;
                if (!visited[x.dest]&&dist[x.dest] > w) {
                    dist[x.dest] = w;
                    pq.add(new node(x.dest, dist[x.dest]));
                    prev[x.dest] = e.dest;
                }
            }
        }

        if (dist[n] == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            print(n);
            }

        }

    static void print(int i) {
        if(i==0)
            return;
        print(prev[i]);
        System.out.print(i+ " " );

    }
    }

class node implements Comparable<node>{
    int dest;int weight;
    node(int dest,int weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return weight-o.weight;

    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt(), c = console.nextInt();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            print(n);
            System.out.print(n);
        }

    }
    static void print(int i){
        if(prev[i]==0)
            return;
        print(prev[i]);
        System.out.print(prev[i]+ " ");
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
       return Long.compare(o.weight,this.weight);
    }
}


import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Scanner;

public class dj {
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node n = pq.remove();

            for (node x:g[n.dest]
                 ) {
                if(dist[x.dest]>dist[n.dest]+x.weight){
                    dist[x.dest]=dist[n.dest]+x.weight;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=n.dest;
                }

            }
        }

        if(dist[n]==Long.MAX_VALUE){
            System.out.println(-1);
        }
        else {
            System.out.print(1+ " ");
            print(n);

        }

    }
    static void print(int i){
       if(prev[i]!=0)
       {
           print(prev[i]);
           System.out.print(i+ " ");
       }
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
       return Long.compare(o.weight,this.weight);
    }
}


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

public class dj {
    static PrintWriter out=new PrintWriter(System.out);
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n,m;
    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();long  c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
        out.close();

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[s]=0;
        pq.add(new node(s,0));
        while(!pq.isEmpty()){
            node e = pq.remove();

            for (node x:g[e.dest]
                    ) {
                long w = dist[e.dest]+x.weight;
                if(dist[x.dest]>w){
                    dist[x.dest]=w;
                    pq.add(new node(x.dest,dist[x.dest]));
                    prev[x.dest]=e.dest;
                }
            }
        }

        if(dist[n]==Long.MAX_VALUE){
            out.println(-1);
        }
        else {
            out.print(1+ " ");
            print(n);

        }

    }
    static void print(int i){
        if(prev[i]!=0)
        {
            print(prev[i]);
            out.print(i+ " ");
        }
    }
}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


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

public class dj {
    static PrintWriter out = new PrintWriter(System.out);
    static ArrayList<node> g[];
    static long[] dist;
    static int[] prev;
    static int n, m;

    static public void main(String args[]) {
        Scanner console = new Scanner(System.in);
        n = console.nextInt();
        m = console.nextInt();
        g = new ArrayList[n + 1];
        dist = new long[n + 1];
        prev = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            g[i] = new ArrayList<>();
            dist[i] = Long.MAX_VALUE;
            prev[i] = 0;
        }
        for (int i = 0; i < m; i++) {
            int a = console.nextInt(), b = console.nextInt();
            long c = console.nextLong();
            g[a].add(new node(b, c));
            g[b].add(new node(a, c));
        }
        dijkstra(1);
        out.close();

    }

    static void dijkstra(int s) {
        PriorityQueue<node> pq = new PriorityQueue<>();
        dist[n] = 0;
        pq.add(new node(n, 0));
        while (!pq.isEmpty()) {
            node e = pq.remove();

            for (node x : g[e.dest]
                    ) {
                long w = dist[e.dest] + x.weight;
                if (dist[x.dest] > w) {
                    dist[x.dest] = w;
                    pq.add(new node(x.dest, dist[x.dest]));
                    prev[x.dest] = e.dest;
                }
            }
        }

        if (dist[1] == Long.MAX_VALUE) {
            out.println(-1);
        } else {
            System.out.print(1 + " ");
            int i = 1;
            while (prev[i] != 0) {
                System.out.print(prev[i] + " ");
                i = prev[i];
            }

        }

    }

}
class node implements Comparable<node>{
    int dest;long weight;
    node(int dest,long weight){
        this.dest=dest;
        this.weight=weight;
    }

    @Override
    public int compareTo(node o) {
        return Long.compare(o.weight,this.weight);
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            if (current.id == nodes.size()) {
                break;
            }

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current;
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            Node previous = end;

            while (previous != start) {
                path.push(previous.id + 1);
                previous = previous.previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
    }

    public static void printPath(Stack<Integer> path) {
        StringBuffer pathOutput = new StringBuffer();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    Node previous = null;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        findShortestPath(nodes);

    }

    public static void findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        //Stack<Integer> path = new Stack<>();
        StringBuilder path = new StringBuilder();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.append((previous +1));
                path.append(" ");//path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }
            path.append(start.id +1);

        } else {
            path.append(-1);
        }

        path = path.reverse();
        System.out.println(path.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        HashMap<Integer, HashMap<Integer, Integer>> weights = new HashMap<>();
        for(int i = 0; i < numNodes; i++) {
            weights.put(i, new HashMap<>());
        }

        Node beg = new Node(0, 0);
        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            weights.get(start).put(end, cost);
            weights.get(end).put(start, cost);
        }

        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        HashSet<Integer> visited = new HashSet<>();
        toVisit.add(beg);
        boolean found = false;
        while (!toVisit.isEmpty()) {
            Node current = toVisit.remove();

            if (visited.contains(current.id)) {
                continue;
            }

            visited.add(current.id);
            if(current.id == numNodes-1) {
                found = true;
                printOutput(current);
                break;
            }
            for (Integer node: weights.get(current.id).keySet()) {
                if(visited.contains(node)) {
                    continue;
                }

                toVisit.add(new Node(node, current.cost + weights.get(current.id).get(node), current));
            }
        }

        if (!found) {
            System.out.println(-1);
        }

    }

    public static void printOutput(Node node) {
        Stack<Integer> path = new Stack<>();
        StringBuffer pathOutput = new StringBuffer();

        while(node.previous != null) {
            path.push(node.id + 1);
            node = node.previous;
        }
        path.push(node.id+1);
        while(!path.isEmpty()) {
            pathOutput.append(path.pop() + " ");
        }
        System.out.println(pathOutput.toString());
    }

}

class Node implements Comparable<Node>{
    int id;
    int cost;
    Node previous = null;

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }
    public Node(int id, int cost, Node prev) {
        this.id = id;
        this.cost = cost;
        this.previous = prev;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            if (current.id == nodes.size()) {
                break;
            }

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current;
                    //toVisit.add(realNode);
                }

                if (!realNode.visited) {
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            Node previous = end;

            while (previous != start) {
                path.push(previous.id + 1);
                previous = previous.previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
    }

    public static void printPath(Stack<Integer> path) {
        StringBuffer pathOutput = new StringBuffer();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    Node previous = null;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            if (current.id == nodes.size()) {
                break;
            }

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current;
                    toVisit.add(realNode);
                }

            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            Node previous = end;

            while (previous != start) {
                path.push(previous.id + 1);
                previous = previous.previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
    }

    public static void printPath(Stack<Integer> path) {
        StringBuffer pathOutput = new StringBuffer();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    Node previous = null;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                }

                if (!realNode.visited) {
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
        //System.out.println(nodes.get(nodes.size()-1).cost);
    }

    public static void printPath(Stack<Integer> path) {
        StringBuilder pathOutput = new StringBuilder();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        HashMap<Integer, HashMap<Integer, Integer>> weights = new HashMap<>();
        for(int i = 0; i < numNodes; i++) {
            weights.put(i, new HashMap<>());
        }

        Node beg = new Node(0, 0);
        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            weights.get(start).put(end, cost);
            weights.get(end).put(start, cost);
        }

        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        HashSet<Integer> visited = new HashSet<>();
        toVisit.add(beg);
        boolean found = false;
        while (!toVisit.isEmpty()) {
            Node current = toVisit.remove();

            if (visited.contains(current.id)) {
                continue;
            }

            visited.add(current.id);
            if(current.id == numNodes-1) {
                found = true;
                printPath(current);
                break;
            }
            for (Integer node: weights.get(current.id).keySet()) {
                if(visited.contains(node)) {
                    continue;
                }

                toVisit.add(new Node(node, current.cost + weights.get(current.id).get(node), current));
            }
        }

        if (!found) {
            System.out.println(-1);
        }

    }

    public static void printPath(Node node) {
        Stack<Integer> path = new Stack<>();
        StringBuffer pathOutput = new StringBuffer();

        while(node.previous != null) {
            path.push(node.id + 1);
            node = node.previous;
        }
        path.push(node.id+1);
        while(!path.isEmpty()) {
            pathOutput.append(path.pop() + " ");
        }
        System.out.println(pathOutput.toString());
    }

}

class Node implements Comparable<Node>{
    int id;
    int cost;
    Node previous = null;

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }
    public Node(int id, int cost, Node prev) {
        this.id = id;
        this.cost = cost;
        this.previous = prev;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        findShortestPath(nodes);

    }

    public static void findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        //Stack<Integer> path = new Stack<>();
        StringBuffer path = new StringBuffer();
        if (end.visited) {
            //path.append(end.id);
            int previous = end.id;

            while (previous != start.id) {
                path.insert(0, " ");
                path.insert(0, (previous +1));//path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.insert(0, " ");
            path.insert(0, (start.id +1));

        } else {
            path.append(-1);
        }

        System.out.println(path.toString());
    }

    public static void printPath(Stack<Integer> path) {
        StringBuilder pathOutput = new StringBuilder();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            if (current.id == nodes.size()) {
                break;
            }

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
    }

    public static void printPath(Stack<Integer> path) {
        StringBuffer pathOutput = new StringBuffer();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        findShortestPath(nodes);

    }

    public static void findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        //Stack<Integer> path = new Stack<>();
        StringBuilder path = new StringBuilder();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.append((previous +1));
                path.append(" ");//path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }
            path.append(start.id +1);

            path = path.reverse();
        } else {
            path.append(-1);
        }

        System.out.println(path.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode = new Node(end, cost);
            nodes.get(start).neighbors.add(newNode);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                }

                if (!realNode.visited) {
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
        //System.out.println(nodes.get(nodes.size()-1).cost);
    }

    public static void printPath(Stack<Integer> path) {
        StringBuilder pathOutput = new StringBuilder();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        findShortestPath(nodes);

    }

    public static void findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        //Stack<Integer> path = new Stack<>();
        StringBuilder path = new StringBuilder();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.insert(0, " ");
                path.insert(0, (previous +1));//path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.insert(0, " ");
            path.insert(0, (start.id +1));

        } else {
            path.append(-1);
        }

        System.out.println(path.toString());
    }

    public static void printPath(Stack<Integer> path) {
        StringBuilder pathOutput = new StringBuilder();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
    }

    public static void printPath(Stack<Integer> path) {
        StringBuffer pathOutput = new StringBuffer();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        ArrayList<Node> nodes = new ArrayList<>();
        for (int i = 0; i < numNodes; i++) {
            nodes.add(new Node(i, Integer.MAX_VALUE));
        }

        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            Node newNode1 = new Node(end, cost);
            Node newNode2 = new Node(start, cost);
            nodes.get(start).neighbors.add(newNode1);
            nodes.get(end).neighbors.add(newNode2);
        }

        Stack<Integer> shortestPath = findShortestPath(nodes);
        printPath(shortestPath);

    }

    public static Stack<Integer> findShortestPath(List<Node> nodes) {
        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        Node start = nodes.get(0);
        Node end = nodes.get(nodes.size()-1);
        toVisit.add(start);
        start.cost = 0;

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

            for (Node node: current.neighbors) {
                Node realNode = nodes.get(node.id);
                int newCost = node.cost + current.cost;
                if (newCost < realNode.cost) {
                    realNode.cost = newCost;
                    realNode.previous = current.id;
                    toVisit.add(realNode);
                }
            }
        }

        Stack<Integer> path = new Stack<>();
        if (end.visited) {
            int previous = end.id;

            while (previous != start.id) {
                path.push(previous + 1);
                previous = nodes.get(previous).previous;
            }

            path.push(start.id+1);

        } else {
            path.push(-1);
        }

        return path;
        //System.out.println(nodes.get(nodes.size()-1).cost);
    }

    public static void printPath(Stack<Integer> path) {
        StringBuilder pathOutput = new StringBuilder();

        while (!path.isEmpty()) {
            pathOutput.append(path.pop());
            pathOutput.append(" ");
        }

        System.out.println(pathOutput.toString());
    }
}

class Node implements Comparable<Node>{
    int id;
    int cost;
    boolean visited = false;
    int previous = 0;
    ArrayList<Node> neighbors = new ArrayList<>();

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

public class CF3 {
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        int numNodes = scanner.nextInt();
        int numEdges = scanner.nextInt();

        HashMap<Integer, HashMap<Integer, Integer>> weights = new HashMap<>();
        for(int i = 0; i < numNodes; i++) {
            weights.put(i, new HashMap<>());
        }

        Node beg = null;
        for (int i = 0; i < numEdges; i++) {
            int start = scanner.nextInt() - 1;
            int end = scanner.nextInt() - 1;
            int cost = scanner.nextInt();

            if(start == 0) {
                beg = new Node(start, 0);
            }
            weights.get(start).put(end, cost);
            weights.get(end).put(start, cost);
        }

        PriorityQueue<Node> toVisit = new PriorityQueue<>();
        HashSet<Integer> visited = new HashSet<>();
        toVisit.add(beg);
        boolean found = false;
        while (!toVisit.isEmpty()) {
            Node current = toVisit.remove();

            if (visited.contains(current.id)) {
                continue;
            }

            visited.add(current.id);
            if(current.id == numNodes-1) {
                found = true;
                printOutput(current);
                break;
            }
            for (Integer node: weights.get(current.id).keySet()) {
                if(visited.contains(node)) {
                    continue;
                }

                toVisit.add(new Node(node, current.cost + weights.get(current.id).get(node), current));
            }
        }

        if (!found) {
            System.out.println(-1);
        }

    }

    public static void printOutput(Node node) {
        Stack<Integer> path = new Stack<>();
        StringBuffer pathOutput = new StringBuffer();

        while(node.previous != null) {
            path.push(node.id + 1);
            node = node.previous;
        }
        path.push(node.id+1);
        while(!path.isEmpty()) {
            pathOutput.append(path.pop() + " ");
        }
        System.out.println(pathOutput.toString());
    }

}

class Node implements Comparable<Node>{
    int id;
    int cost;
    Node previous = null;

    public Node(int id, int cost) {
        this.id = id;
        this.cost = cost;
    }
    public Node(int id, int cost, Node prev) {
        this.id = id;
        this.cost = cost;
        this.previous = prev;
    }

    public int compareTo(Node other) {
        return cost < other.cost ? -1 : 1;
    }
}


import java.util.*;

/**
 * Created by carolineshi on 2/4/17.
 */
public class Dijkstra {

    public static class Node implements Comparable<Node> {
        int val;
        long cost;
        HashSet<Edge> edges;
        boolean visited;
        Node parent;

        public Node(int val) {
            this.val = val;
            this.cost = Long.MAX_VALUE;
            this.edges = new HashSet<>();
            this.visited = false;
        }

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

    public static class Edge {
        int length;
        Node dest;

        public Edge(int l, Node d) {
            this.length = l;
            this.dest = d;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int nodes = scan.nextInt();
        int edges = scan.nextInt();
        HashMap<Integer, Node> graph = new HashMap();

        for(int i = 0; i < edges; i++) {
            int n = scan.nextInt();
            int q = scan.nextInt();
            Node a = graph.getOrDefault(n, new Node(n));
            Node b = graph.getOrDefault(q, new Node(q));

            int length = scan.nextInt();
            a.edges.add(new Edge(length, b));
            b.edges.add(new Edge(length, a));
            graph.put(n, a);
            graph.put(q, b);
        }

        Node c = graph.get(1);
        dijkstra(c);
        String done = "";
        Node end = graph.get(nodes);
        while (end != null && end.val != 1) {
            done = end.val + " " + done;
            end = end.parent;
        }
        if(end!=null) {
            done = 1 + " " + done;
        }
        System.out.println(done);

    }

    public static void dijkstra(Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        if(start==null){
            return;
        }

        start.cost = 0;
        start.parent = null;
        start.visited = true;
        pq.offer(start);

//        while(!pq.isEmpty()) {
//            Node cur = pq.poll();
//
//            for(Edge e: cur.edges) {
//                Node next = e.dest;
//                long newCost = cur.cost + e.length;
//                if (!next.visited && newCost < next.cost) {
//                    next.cost = newCost;
//                    next.visited = true;
//                    next.parent = cur;
//                    pq.add(next);
//                }
//            }
//
//        }

        while(!pq.isEmpty()) {
            Node cur = pq.poll();
            if(!cur.visited) {
                cur.visited = true;
                for (Edge s : cur.edges) {
                    long newCost = cur.cost + s.length;
                    if (!s.dest.visited && newCost < s.dest.cost) {
                        if(newCost < s.dest.cost) {
//                            System.out.println("new cost of " + newCost + " at Node " + s.dest.val);
                            if(s.dest.cost < 999999) {
                                pq.remove(s.dest);
                            }
                            s.dest.cost = newCost;
                            s.dest.parent = cur;
                            pq.add(s.dest);
                        }
                    }
                }
            }
        }
    }

}


import java.util.*;

/**
 * Created by carolineshi on 2/4/17.
 */
public class Dijkstra {

    public static class Node implements Comparable<Node> {
        int val;
        long cost;
        HashSet<Edge> edges;
        boolean visited;
        Node parent;

        public Node(int val) {
            this.val = val;
            this.cost = Long.MAX_VALUE;
            this.edges = new HashSet<>();
            this.visited = false;
        }

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

    public static class Edge {
        int length;
        Node dest;

        public Edge(int l, Node d) {
            this.length = l;
            this.dest = d;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int nodes = scan.nextInt();
        int edges = scan.nextInt();
        HashMap<Integer, Node> graph = new HashMap();

        for(int i = 0; i < edges; i++) {
            int n = scan.nextInt();
            int q = scan.nextInt();
            Node a = graph.getOrDefault(n, new Node(n));
            Node b = graph.getOrDefault(q, new Node(q));

            int length = scan.nextInt();
            a.edges.add(new Edge(length, b));
            b.edges.add(new Edge(length, a));
            graph.put(n, a);
            graph.put(q, b);
        }

        Node c = graph.get(1);
        dijkstra(c);

//        int[] done = new int[nodes];
//        StringBuilder s = new StringBuilder();
        Node end = graph.get(nodes);

        ArrayList<Integer> list = new ArrayList<>();
        if(end==null) {
            System.out.println("-1");
        }
        while(end != null) {
            list.add(end.val);
            end = end.parent;
        }

        if(!list.contains(1)) {
            System.out.println("-1");
        } else {
            Collections.reverse(list);
            for (int g : list) {
                System.out.print(g + " ");
            }
        }

//        int index = 0;
//        Node prev = null;

//        while(end != null) {
//            done[index] = end.val;
//            prev = end;
//            end = end.parent;
//            index++;
//        }
//        if(prev != null && prev.val == 1) {
//            int i = 0;
//            while(i < nodes && done[i] != 0) {
//                s.insert(0, " ");
//                s.insert(0, done[i]);
//                i++;
//            }
//        } else {
//            s.append("-1");
//        }

    }

    public static void dijkstra(Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        if(start==null){
            return;
        }

        start.cost = 0;
        start.parent = null;
        start.visited = true;
        pq.offer(start);

        while(!pq.isEmpty()) {
            Node cur = pq.poll();
            cur.visited = true;

            for(Edge e: cur.edges) {
                Node next = e.dest;
                long newCost = cur.cost + e.length;
                if (!next.visited && newCost < next.cost) {
                    next.cost = newCost;
                    next.parent = cur;
//                    System.out.println("Node " + next.val + " cost " + next.cost + " parent " + next.parent.val);
                    pq.add(next);
                }
            }

        }
    }
}


import java.util.*;

/**
 * Created by carolineshi on 2/4/17.
 */
public class Dijkstra {

    public static class Node implements Comparable<Node> {
        int val;
        long cost;
        HashSet<Edge> edges;
        boolean visited;
        Node parent;

        public Node(int val) {
            this.val = val;
            this.cost = Long.MAX_VALUE;
            this.edges = new HashSet<>();
            this.visited = false;
        }

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

    public static class Edge {
        int length;
        Node dest;

        public Edge(int l, Node d) {
            this.length = l;
            this.dest = d;
        }
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int nodes = scan.nextInt();
        int edges = scan.nextInt();
        HashMap<Integer, Node> graph = new HashMap();

        for(int i = 0; i < edges; i++) {
            int n = scan.nextInt();
            int q = scan.nextInt();
            Node a = graph.getOrDefault(n, new Node(n));
            Node b = graph.getOrDefault(q, new Node(q));

            int length = scan.nextInt();
            a.edges.add(new Edge(length, b));
            b.edges.add(new Edge(length, a));
            graph.put(n, a);
            graph.put(q, b);
        }

        Node c = graph.get(1);
        dijkstra(c);
        String done = "";
        Node end = graph.get(nodes);
        while (end != null && end.val != 1) {
            done = end.val + " " + done;
            end = end.parent;
        }
        if(end!=null) {
            done = 1 + " " + done;
        } else {
            done = "-1";
        }
        System.out.println(done);

    }

    public static void dijkstra(Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

        if(start==null){
            return;
        }

        start.cost = 0;
        start.parent = null;
        start.visited = true;
        pq.offer(start);

        while(!pq.isEmpty()) {
            Node cur = pq.poll();
            cur.visited = true;

            for(Edge e: cur.edges) {
                Node next = e.dest;
                long newCost = cur.cost + e.length;
                if (!next.visited && newCost < next.cost) {
                    next.cost = newCost;
                    next.parent = cur;
//                    System.out.println("Setting node " + next.val + " cost to " + next.cost + " and the parent is " + next.parent.val);
                    pq.add(next);
                }
            }

        }

//        while(!pq.isEmpty()) {
//            Node cur = pq.poll();
//            if(!cur.visited) {
//                cur.visited = true;
//                for (Edge s : cur.edges) {
//                    long newCost = cur.cost + s.length;
//                    if (!s.dest.visited && newCost < s.dest.cost) {
//                        if(newCost < s.dest.cost) {
////                            System.out.println("new cost of " + newCost + " at Node " + s.dest.val);
//                            if(s.dest.cost < 999999) {
//                                pq.remove(s.dest);
//                            }
//                            s.dest.cost = newCost;
//                            s.dest.parent = cur;
//                            pq.add(s.dest);
//                        }
//                    }
//                }
//            }
//        }
    }

}