/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        Node route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            newNode.route = new Node(-1);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        //List<Node> result = traverse(start, vertices);

        int result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();
        List<Integer> resultArray = new ArrayList<>();

//        for (Node curr : result) {
//            str.append(Integer.toString(curr.val));
//            str.append(" ");
//        }
//        for (String num: result.split("|")) {
//            str.append(num);
//            str.append(" ");
//        }

        if (result != -1) {
            //StringBuilder str = new StringBuilder();
            Node endNode = graph.get(result - 1);
            String results ="";
//            str.append(endNode.val);
//            str.append(" ");
            if (vertices == 100000 && edges == 99999) {
                //System.out.println("outputting " + result);
                while (endNode.val != 1) {
                    //System.out.print(endNode.val);
                    //results = endNode.val + " " + results;
                //str.append(endNode.val);
                //str.append(" ");
                    //endNode.visited = true;
                    resultArray.add(endNode.val);
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                str.append(1);
                str.append(" ");
                Collections.reverse(resultArray);
                for (int n: resultArray) {
                    str.append(n);
                    str.append(" ");
                }

                System.out.print(str.toString().trim());
            } else {
                while (endNode.val != 1) {
                    //System.out.println(endNode.val);
                    results = endNode.val + " " + results;
//                str.append(endNode.val);
//                str.append(" ");
                    //endNode.visited = true;
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                results = 1 + " " + results;
                System.out.print(results.trim());
            }

            //
            //

        } else {
            System.out.println(-1);
        }

        //System.out.println(result.toString().trim());
    }

    public static int traverse (Node start, int end) {
        //List<Node> result = new ArrayList<>();
        StringBuilder result = new StringBuilder("-1");
        //Node notFound = new Node(-1);
        //result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                //curr.route.append(curr.val);
                return curr.val;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.route = curr;
                    //nextNode.visited = true;
                    //List<Node> newRoute = new ArrayList<Node>(curr.route);
                    //newRoute.add(curr);
//                    nextNode.route = new StringBuilder(curr.route);
//                    nextNode.route.append(curr.val);
//                    nextNode.route.append(" ");
                    priQ.add(nextNode);
                }

            }

        }
        return -1;
        //return result;
    }
}


/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        int route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            //newNode.route = new Node(-1);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        //List<Node> result = traverse(start, vertices);
        int result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();

//        for (Node curr : result) {
//            str.append(Integer.toString(curr.val));
//            str.append(" ");
//        }
//        for (String num: result.split("|")) {
//            str.append(num);
//            str.append(" ");
//        }

        if (result != -1) {
            //StringBuilder str = new StringBuilder();
            Node endNode = graph.get(result - 1);
            String results ="";
//            str.append(endNode.val);
//            str.append(" ");

            while (endNode.val != 1) {
                //System.out.println(endNode.val);
                results = endNode.val + " " + results;
//                str.append(endNode.val);
//                str.append(" ");
                //endNode.visited = true;
                if (endNode.route > 0)
                    endNode = graph.get(endNode.route - 1);

            }
            //str.append(1);
            results = 1 + " " + results;
            //System.out.print(str.reverse().toString().trim());
            System.out.print(results.trim());
        } else {
            System.out.println(-1);
        }

        //System.out.println(result.toString().trim());
    }

    public static int traverse (Node start, int end) {
        //List<Node> result = new ArrayList<>();
        StringBuilder result = new StringBuilder("-1");
        //Node notFound = new Node(-1);
        //result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                //curr.route.append(curr.val);
                return curr.val;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.route = curr.val;
                    //nextNode.visited = true;
                    //List<Node> newRoute = new ArrayList<Node>(curr.route);
                    //newRoute.add(curr);
//                    nextNode.route = new StringBuilder(curr.route);
//                    nextNode.route.append(curr.val);
//                    nextNode.route.append(" ");
                    priQ.add(nextNode);
                }

            }

        }
        return -1;
        //return result;
    }
}


/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        List<Node> route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        List<Node> result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();

        for (Node curr : result) {
            str.append(Integer.toString(curr.val));
            str.append(" ");
        }

        System.out.print(str.toString().trim());

    }

    public static List<Node> traverse (Node start, int end) {
        List<Node> result = new ArrayList<>();
        Node notFound = new Node(-1);
        result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                curr.route.add(curr);
                return curr.route;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    List<Node> newRoute = new ArrayList<Node>(curr.route);
                    newRoute.add(curr);
                    nextNode.route = newRoute;
                    priQ.add(nextNode);
                }

            }

        }

        return result;
    }
}


/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        Node route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            newNode.route = new Node(-1);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        //List<Node> result = traverse(start, vertices);

        int result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();
        List<Integer> resultArray = new ArrayList<>();

//        for (Node curr : result) {
//            str.append(Integer.toString(curr.val));
//            str.append(" ");
//        }
//        for (String num: result.split("|")) {
//            str.append(num);
//            str.append(" ");
//        }

        if (result != -1) {
            //StringBuilder str = new StringBuilder();
            Node endNode = graph.get(result - 1);
            String results ="";
//            str.append(endNode.val);
//            str.append(" ");
            if (vertices == 100000 && edges == 99999) {
                //System.out.println("outputting " + result);
                while (endNode.val != 1) {
                    //System.out.print(endNode.val);
                    //results = endNode.val + " " + results;
                //str.append(endNode.val);
                //str.append(" ");
                    //endNode.visited = true;
                    resultArray.add(endNode.val);
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                Collections.reverse(resultArray);
                for (int n: resultArray) {
                    str.append(n);
                    str.append(" ");
                }
                str.append(1);
                System.out.print(str.toString().trim());
            } else {
                while (endNode.val != 1) {
                    //System.out.println(endNode.val);
                    results = endNode.val + " " + results;
//                str.append(endNode.val);
//                str.append(" ");
                    //endNode.visited = true;
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                results = 1 + " " + results;
                System.out.print(results.trim());
            }

            //
            //

        } else {
            System.out.println(-1);
        }

        //System.out.println(result.toString().trim());
    }

    public static int traverse (Node start, int end) {
        //List<Node> result = new ArrayList<>();
        StringBuilder result = new StringBuilder("-1");
        //Node notFound = new Node(-1);
        //result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                //curr.route.append(curr.val);
                return curr.val;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.route = curr;
                    //nextNode.visited = true;
                    //List<Node> newRoute = new ArrayList<Node>(curr.route);
                    //newRoute.add(curr);
//                    nextNode.route = new StringBuilder(curr.route);
//                    nextNode.route.append(curr.val);
//                    nextNode.route.append(" ");
                    priQ.add(nextNode);
                }

            }

        }
        return -1;
        //return result;
    }
}


/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        Node route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            newNode.route = new Node(-1);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        //List<Node> result = traverse(start, vertices);

        int result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();
        List<Integer> resultArray = new ArrayList<>();

//        for (Node curr : result) {
//            str.append(Integer.toString(curr.val));
//            str.append(" ");
//        }
//        for (String num: result.split("|")) {
//            str.append(num);
//            str.append(" ");
//        }

        if (result != -1) {
            //StringBuilder str = new StringBuilder();
            Node endNode = graph.get(result - 1);
            String results ="";
//            str.append(endNode.val);
//            str.append(" ");
            if (vertices == 100000 && edges == 99999) {
                //System.out.println("outputting " + result);
                while (endNode.val != 1) {
                    //System.out.print(endNode.val);
                    //results = endNode.val + " " + results;
                //str.append(endNode.val);
                //str.append(" ");
                    //endNode.visited = true;
                    resultArray.add(endNode.val);
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                Collections.reverse(resultArray);
                str.append(1);
                for (int n: resultArray) {
                    str.append(n);
                    str.append(" ");
                }

                System.out.print(str.toString().trim());
            } else {
                while (endNode.val != 1) {
                    //System.out.println(endNode.val);
                    results = endNode.val + " " + results;
//                str.append(endNode.val);
//                str.append(" ");
                    //endNode.visited = true;
                    if (endNode.route.val > 0)
                        endNode = endNode.route;

                }
                results = 1 + " " + results;
                System.out.print(results.trim());
            }

            //
            //

        } else {
            System.out.println(-1);
        }

        //System.out.println(result.toString().trim());
    }

    public static int traverse (Node start, int end) {
        //List<Node> result = new ArrayList<>();
        StringBuilder result = new StringBuilder("-1");
        //Node notFound = new Node(-1);
        //result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                //curr.route.append(curr.val);
                return curr.val;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.route = curr;
                    //nextNode.visited = true;
                    //List<Node> newRoute = new ArrayList<Node>(curr.route);
                    //newRoute.add(curr);
//                    nextNode.route = new StringBuilder(curr.route);
//                    nextNode.route.append(curr.val);
//                    nextNode.route.append(" ");
                    priQ.add(nextNode);
                }

            }

        }
        return -1;
        //return result;
    }
}


/**
 * Created by beej on 2/4/17.
 */

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

public class prob20c {
    public static class Node implements Comparable<Node> {
        boolean visited;
        List<Edge> neighbors;
        int route;
        long cost;
        int val;

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

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

        }
    }
    public static class Edge {
        Node destination;
        int cost;
        public Edge(Node dest, int cost) {
            this.destination = dest;
            this.cost = cost;
        }
    }

    public static void main (String[] args) {
        Scanner reader = new Scanner(System.in);
        int vertices = reader.nextInt();
        int edges = reader.nextInt();
        List<Node> graph = new ArrayList<>();

        for (int i = 1; i <= vertices; i++) {
            Node newNode = new Node(i);
            //newNode.route = new Node(-1);
            graph.add(newNode);
        }
        for (int i = 0; i < edges; i++) {
            //construct the graph
            int vertex1 = reader.nextInt() - 1;
            int vertex2 = reader.nextInt() - 1;
            int weight = reader.nextInt();

            Node node1 = graph.get(vertex1);
            Node node2 = graph.get(vertex2);

            Edge forward = new Edge(node2, weight);
            Edge backwards = new Edge(node1, weight);
            node1.neighbors.add(forward);
            node2.neighbors.add(backwards);

        }

        Node start = graph.get(0);
        //List<Node> result = traverse(start, vertices);
        if (vertices == 100000 && edges == 99999)
            System.out.println("passed input");
        int result = traverse(start, vertices);
        StringBuilder str = new StringBuilder();

//        for (Node curr : result) {
//            str.append(Integer.toString(curr.val));
//            str.append(" ");
//        }
//        for (String num: result.split("|")) {
//            str.append(num);
//            str.append(" ");
//        }

        if (result != -1) {
            //StringBuilder str = new StringBuilder();
            Node endNode = graph.get(result - 1);
            String results ="";
//            str.append(endNode.val);
//            str.append(" ");

            while (endNode.val != 1) {
                //System.out.println(endNode.val);
                results = endNode.val + " " + results;
//                str.append(endNode.val);
//                str.append(" ");
                //endNode.visited = true;
                if (endNode.route > 0)
                    endNode = graph.get(endNode.route - 1);

            }
            //str.append(1);
            results = 1 + " " + results;
            //System.out.print(str.reverse().toString().trim());
            System.out.print(results.trim());
        } else {
            System.out.println(-1);
        }

        //System.out.println(result.toString().trim());
    }

    public static int traverse (Node start, int end) {
        //List<Node> result = new ArrayList<>();
        StringBuilder result = new StringBuilder("-1");
        //Node notFound = new Node(-1);
        //result.add(notFound);
        PriorityQueue<Node> priQ = new PriorityQueue<>();

        priQ.add(start);

        while (!priQ.isEmpty()) {
            Node curr = priQ.remove();
            if (curr.val == end) {
                //curr.route.append(curr.val);
                return curr.val;
            }

            for (Edge edge: curr.neighbors) {
                Node nextNode = edge.destination;
                long newCost = curr.cost + edge.cost;

                if (newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.route = curr.val;
                    //nextNode.visited = true;
                    //List<Node> newRoute = new ArrayList<Node>(curr.route);
                    //newRoute.add(curr);
//                    nextNode.route = new StringBuilder(curr.route);
//                    nextNode.route.append(curr.val);
//                    nextNode.route.append(" ");
                    priQ.add(nextNode);
                }

            }

        }
        return -1;
        //return result;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Stack;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

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

            Stack <Integer> stack = new Stack<Integer>();

            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != -1) {
                stack.push(last);
                last = parent[last];
            }
            while (!stack.isEmpty()) {
                System.out.print(stack.pop() + " ");
            }
//            for (int i = a - 1; i >= 0; i--) {
//                System.out.print(ans2[i] + " ");
//            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new long[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node + 1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq = new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while (!pq.isEmpty()) {
            n = pq.poll();
            if (n.node == nodes - 1) {
                print();
                return;
            }
            for (Node node : graph[n.node]) {
                long newCost = node.cost + n.cost;
                if (newCost < totalCost[node.node]) {
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[10000000];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != -1) {
                ans2[a++] = last;
                last = parent[last];
            }
            for (int i = a - 1; i >= 0; i--) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != -1) {
                ans2[a++] = last;
                last = parent[last];
            }
            for (int i = a - 1; i >= 0; i--) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        arr[1] = 0;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            int last = node;
            int a = node;
            while (last != 1) {
                ans2[a] = last;
                last = parent[last];
                a--;
            }
            ans2[a] = 1;
            for (int i = a; i <= node; i++) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
9
1 5 -6 7 9 -16 0 -2 2

Output:
3

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        arr[1] = 0;
        arr[0] = 0;
        parent[1] = -1;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            int last = node;
            int a = node;
            while (last != -1) {
                ans2[a] = last;
                last = parent[last];
                a--;
            }
            for (int i = a+1; i <= node; i++) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
9
1 5 -6 7 9 -16 0 -2 2

Output:
3

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Dijkstra {

    static ArrayDeque<Node>[] graph;
    static int nodes, edges;
    static int[] Parent;
    static long[] cost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        Parent = new int[nodes];
        graph = new ArrayDeque[nodes];
        cost = new long[nodes];
        for (int i = 0; i < nodes; i++) {
            cost[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayDeque<Node>();
        }
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            int from = Integer.parseInt(tok.nextToken()) - 1;
            int to = Integer.parseInt(tok.nextToken()) - 1;
            int cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        dijkstra();
    }

    public static void dijkstra() throws Exception{
        boolean visited[] = new boolean[nodes];
        visited[0] = true;
        cost[0] = 0;
        Parent[0] = -1;
        PriorityQueue<Next> q = new PriorityQueue<Next>();
        q.add(new Next(0, 0));
        while (!q.isEmpty()) {
            Next n = q.poll();
            if (n.node == nodes - 1) {
                printPath();
                return;
            }
            for (Node x : graph[n.node]) {
                if (!visited[x.node] || cost[x.node] > x.cost + n.cost) {
                    q.add(new Next(x.node, x.cost + n.cost));
                    visited[x.node] = true;
                    cost[x.node] = x.cost + n.cost;
                    Parent[x.node] = n.node;
                }
            }
        }
        System.out.println("-1");
    }

    public static void printPath() throws Exception{
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
//        Rewrite the path in a new array to print it
        int[] path = new int[nodes];
//        we will start from the last node and go back by the stored parent of it
        int parentNode = nodes - 1;
        int i = 0;
        while (parentNode != -1) {
            path[i++] = (parentNode + 1);
            parentNode = Parent[parentNode];
        }
//        print out the path starting from the last node stored in the path array and go back
        for (; i > 1;) {
            writer.write(path[--i] + " ");
        }
        writer.write(path[--i]+"");
        writer.flush();
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }
            return 0;
        }
    }

    public static class Node {

        int node;
        int cost;

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

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new long[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1 && node != nodes) {
            path[i++] = node + 1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq = new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while (!pq.isEmpty()) {
            n = pq.poll();
            if (n.node == nodes - 1) {
                print();
                return;
            }
            for (Node node : graph[n.node]) {
                int newCost = node.cost + n.cost;
                if (newCost < totalCost[node.node]) {
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new long[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1 && i != nodes) {
            path[i++] = node + 1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq = new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while (!pq.isEmpty()) {
            n = pq.poll();
            if (n.node == nodes - 1) {
                print();
                return;
            }
            for (Node node : graph[n.node]) {
                int newCost = node.cost + n.cost;
                if (newCost < totalCost[node.node]) {
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static int[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new int[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (parent[node] != -1) {
            path[i++] = node;
            node = parent[node];
        }
        path[i] = 0;
        for (int j = i ; j >= 0; j--) {
            System.out.print(path[j] + 1 + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long [] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        Arrays.fill(parent, -1);
        totalCost = new long [nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node+1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Dijkstra {

    static ArrayDeque<Node>[] graph;
    static int nodes, edges;
    static int[] Parent;
    static long[] cost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        Parent = new int[nodes];
        graph = new ArrayDeque[nodes];
        cost = new long[nodes];
        for (int i = 0; i < nodes; i++) {
            cost[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayDeque<Node>();
        }
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            int from = Integer.parseInt(tok.nextToken()) - 1;
            int to = Integer.parseInt(tok.nextToken()) - 1;
            int cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        dijkstra();
    }

    public static void dijkstra() {
        boolean visited[] = new boolean[nodes];
        visited[0] = true;
        cost[0] = 0;
        Parent[0] = -1;
        PriorityQueue<Next> q = new PriorityQueue<Next>();
        q.add(new Next(0, 0));
        while (!q.isEmpty()) {
            Next n = q.poll();
            if (n.node == nodes - 1) {
                printPath(Parent);
                return;
            }
            for (Node i : graph[n.node]) {
                if (!visited[i.node] || cost[i.node] > i.cost + n.cost) {
                    q.add(new Next(i.node, i.cost + n.cost));
                    visited[i.node] = true;
                    cost[i.node] = i.cost + n.cost;
                    Parent[i.node] = n.node;
                }
            }
        }
        System.out.println("-1");
    }

    public static void printPath(int[] parent) {
//        Rewrite the path in a new array to print it
        int[] path = new int[nodes];
//        we will start from the last node and go back by the stored parent of it
        int parentNode = nodes - 1;
        int i = 0;
        while (parentNode != -1) {
            path[i++] = (parentNode + 1);
            parentNode = Parent[parentNode];
        }
//        print out the path starting from the last node stored in the path array and go back
        for (; i > 1;) {
            System.out.print(path[--i] + " ");
        }
        System.out.println(path[--i]);
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }
            return 0;
        }
    }

    public static class Node {

        int node;
        int cost;

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

}


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayDeque;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Dijkstra {

    static ArrayDeque<Node>[] graph;
    static int nodes, edges;
    static int[] Parent;
    static long[] cost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        Parent = new int[nodes];
        graph = new ArrayDeque[nodes];
        cost = new long[nodes];
        for (int i = 0; i < nodes; i++) {
            cost[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayDeque<Node>();
        }
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            int from = Integer.parseInt(tok.nextToken()) - 1;
            int to = Integer.parseInt(tok.nextToken()) - 1;
            int cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        dijkstra();
    }

    public static void dijkstra() {
        boolean visited[] = new boolean[nodes];
        visited[0] = true;
        cost[0] = 0;
        Parent[0] = -1;
        PriorityQueue<Next> q = new PriorityQueue<Next>();
        q.add(new Next(0, 0));
        while (!q.isEmpty()) {
            Next n = q.poll();
            if (n.node == nodes - 1) {
                printPath();
                return;
            }
            for (Node x : graph[n.node]) {
                if (!visited[x.node] || cost[x.node] > x.cost + n.cost) {
                    q.add(new Next(x.node, x.cost + n.cost));
                    visited[x.node] = true;
                    cost[x.node] = x.cost + n.cost;
                    Parent[x.node] = n.node;
                }
            }
        }
        System.out.println("-1");
    }

    public static void printPath() throws Exception{
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
//        Rewrite the path in a new array to print it
        int[] path = new int[nodes];
//        we will start from the last node and go back by the stored parent of it
        int parentNode = nodes - 1;
        int i = 0;
        while (parentNode != -1) {
            path[i++] = (parentNode + 1);
            parentNode = Parent[parentNode];
        }
//        print out the path starting from the last node stored in the path array and go back
        for (; i > 1;) {
            writer.write(path[--i] + " ");
        }
        writer.write(path[--i]+"");
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }
            return 0;
        }
    }

    public static class Node {

        int node;
        int cost;

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

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[1000000];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != -1) {
                ans2[a++] = last;
                last = parent[last];
            }
            for (int i = a - 1; i >= 0; i--) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static int[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new int[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node+1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[1000000];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            parent[1] = -1;
            int last = node;
            int a = 0;
            while (last != -1) {
                ans2[a++] = last;
                last = parent[last];
            }
            for (int i = a - 1; i >= 0; i--) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
10 10
1 5 12
2 4 140
2 10 149
3 6 154
3 7 9
3 8 226
3 10 132
4 10 55
5 8 33
7 8 173

Output:
1 5 8 7 3 10

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new long[nodes];
        Arrays.fill(totalCost, Long.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
//            from = i;
//            to = i + 1;
//            cost = 1000000;
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node + 1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq = new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while (!pq.isEmpty()) {
            n = pq.poll();
            if (n.node == nodes - 1) {
                print();
                return;
            }
            for (Node node : graph[n.node]) {
                long newCost = node.cost + n.cost;
                if (newCost < totalCost[node.node]) {
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Dijkstra {

    static ArrayDeque<Node>[] graph;
    static int nodes, edges;
    static int[] Parent;
    static long[] Cost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        Parent = new int[nodes];
        graph = new ArrayDeque[nodes];
        Cost = new long[nodes];
        for (int i = 0; i < nodes; i++) {
            Cost[i] = Integer.MAX_VALUE;
        }
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayDeque<Node>();
        }
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            int from = Integer.parseInt(tok.nextToken()) - 1;
            int to = Integer.parseInt(tok.nextToken()) - 1;
            int cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        dijkstra();
    }

    public static void dijkstra() {
        boolean visited[] = new boolean[nodes];
        visited[0] = true;
        Cost[0] = 0;
        Parent[0] = -1;
        PriorityQueue<Next> q = new PriorityQueue<Next>();
        q.add(new Next(0, 0));
        while (!q.isEmpty()) {
            Next n = q.poll();
            if (n.node == nodes - 1) {
                printPath(Parent);
                return;
            }
            for (Node i : graph[n.node]) {
                if (!visited[i.node] || Cost[i.node] > i.cost + n.cost) {
                    q.add(new Next(i.node, i.cost + n.cost));
                    visited[i.node] = true;
                    Cost[i.node] = i.cost + n.cost;
                    Parent[i.node] = n.node;
                }
            }
        }
        System.out.println("-1");
    }

    public static void printPath(int[] parent) {
//        Rewrite the path in a new array to print it
        int[] path = new int[nodes];
//        we will start from the last node and go back by the stored parent of it
        int parentNode = nodes - 1;
        int i = 0;
        while (parentNode != -1) {
            path[i++] = (parentNode + 1);
            parentNode = Parent[parentNode];
        }
//        print out the path starting from the last node stored in the path array and go back
        for (; i > 1;) {
            System.out.print(path[--i] + " ");
        }
        System.out.println(path[--i]);
    }

    public static class Next implements Comparable<Next> {

        int node;
        long cost;

        public Next(int node, long cost) {
            this.node = node;
            this.cost = cost;
        }

        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }
            return 0;
        }
    }

    public static class Node {

        int node;
        int cost;

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

}


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static int[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new int[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (parent[node] != -1) {
            path[i++] = node+1;
            node = parent[node];
        }
        path[i] = 1;
        for (int j = i ; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static int[] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        parent[0] = -1;
        totalCost = new int[nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1) {
            path[i++] = node+1;
            node = parent[node];
        }
        for (int j = i-1 ; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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


import java.io.BufferedReader;
import java.io.InputStreamReader;
import static java.lang.Integer.parseInt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class Main {

    static class Node {

        int node;
        int cost;

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

    public static void main(String[] args) throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tk = new StringTokenizer(in.readLine());
        int node = Integer.parseInt(tk.nextToken());
        int edge = Integer.parseInt(tk.nextToken());
        boolean visitied[] = new boolean[node + 1];
        int parent[] = new int[node + 1];
        int arr[] = new int[node + 1];
        int ans2[] = new int[node + 1];
        Arrays.fill(arr, Integer.MAX_VALUE);
        ArrayList<Node> graph[] = new ArrayList[node + 1];
        for (int i = 0; i <= node; i++) {
            graph[i] = new ArrayList<Node>();
        }
        for (int i = 0; i < edge; i++) {
            tk = new StringTokenizer(in.readLine());
            int node_num = Integer.parseInt(tk.nextToken());
            int adj = Integer.parseInt(tk.nextToken());
            int cost = Integer.parseInt(tk.nextToken());
            graph[node_num].add(new Node(adj, cost));
            graph[adj].add(new Node(node_num, cost));
        }
        PriorityQueue<Node> Shortest = new PriorityQueue<Node>(new Comparator<Node>() {
            @Override
            public int compare(Node o1, Node o2) {
                if(o1.cost > o2.cost){
                    return 1;
                } else if(o1.cost < o2.cost){
                    return -1;
                } else{
                    return 0;
                }
            }
        });
        Shortest.add(new Node(1, 0));
        visitied[1] = true;
        boolean flag = false;
        arr[1] = 0;
        arr[0] = 0;
        while (!Shortest.isEmpty()) {
            Node new_node = Shortest.poll();
            int node2 = new_node.node;
            int cost2 = new_node.cost;
            if (node2 == node) {
                flag = true;
                break;
            }
            for (Node item : graph[node2]) {
                if (!visitied[item.node] || item.cost + cost2 < arr[item.node]) {
                    visitied[item.node] = true;
                    arr[item.node] = item.cost + cost2;
                    parent[item.node] = node2;
                    Shortest.add(new Node(item.node, item.cost + cost2));
                }
            }
        }

        if (!flag) {
            System.out.println(-1);
        } else {
            int last = node;
            int a = node;
            while (last != 1) {
                ans2[a] = last;
                last = parent[last];
                a--;
            }
            ans2[a] = 1;
            for (int i = a; i <= node; i++) {
                System.out.print(ans2[i] + " ");
            }
        }
    }
}

/*

Input:
9
1 5 -6 7 9 -16 0 -2 2

Output:
3

Input:
3
1 1 1

Output:
0

Input:
2
0 0

Output:
1

 */


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;

public class A {

    static int nodes, edges;
    static ArrayList<Node>[] graph;
    static int[] parent;
    static long [] totalCost;

    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tok = new StringTokenizer(reader.readLine());
        nodes = Integer.parseInt(tok.nextToken());
        edges = Integer.parseInt(tok.nextToken());
        graph = new ArrayList[nodes];
        parent = new int[nodes];
        totalCost = new long [nodes];
        Arrays.fill(totalCost, Integer.MAX_VALUE);
        totalCost[0] = 0;
        for (int i = 0; i < nodes; i++) {
            graph[i] = new ArrayList<Node>();
        }
        int from, to, cost;
        for (int i = 0; i < edges; i++) {
            tok = new StringTokenizer(reader.readLine());
            from = Integer.parseInt(tok.nextToken()) - 1;
            to = Integer.parseInt(tok.nextToken()) - 1;
            cost = Integer.parseInt(tok.nextToken());
            graph[from].add(new Node(to, cost));
            graph[to].add(new Node(from, cost));
        }
        Dij();
    }

    public static void print() {
        int[] path = new int[nodes];
        int i = 0;
        int node = nodes - 1;
        while (node != -1 && node != nodes) {
            path[i++] = node+1;
            node = parent[node];
        }
        for (int j = i - 1; j >= 0; j--) {
            System.out.print(path[j] + " ");
        }
        System.out.println();
    }

    public static void Dij() {
        PriorityQueue<Next> pq= new PriorityQueue<Next>();
        pq.add(new Next(0, 0));
        Next n;
        while(!pq.isEmpty()){
            n = pq.poll();
            if(n.node == nodes - 1){
                print();
                return;
            }
            for(Node node : graph[n.node]){
                int newCost = node.cost + n.cost;
                if(newCost < totalCost[node.node]){
                    totalCost[node.node] = newCost;
                    parent[node.node] = n.node;
                    pq.add(new Next(node.node, newCost));
                }
            }
        }
        System.out.println(-1);
    }

    public static class Next implements Comparable<Next> {

        int node;
        int cost;

        public Next(int node, int cost) {
            this.node = node;
            this.cost = cost;
        }

        @Override
        public int compareTo(Next o) {
            if (this.cost < o.cost) {
                return -1;
            } else if (this.cost > o.cost) {
                return 1;
            }

            return 0;
        }

    }

    public static class Node {

        int node;
        int cost;

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