import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    }

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

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

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

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

}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        if(start==null){
            return;
        }

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

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

}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        start.cost = 0;
        start.parent = null;
        pq.offer(start);

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

}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        start.cost = 0;
        start.parent = null;
        pq.offer(start);

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

}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

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

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

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

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

        if(start==null){
            return;
        }

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

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

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

        }
    }
}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        start.cost = 0;
        start.parent = null;
        pq.offer(start);

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

}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

        int[] done = new int[nodes];
        StringBuilder s = new StringBuilder();
        Node end = graph.get(nodes);
        int index = 0;
        Node prev = null;

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

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

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

        if(start==null){
            return;
        }

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

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

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

        }
    }
}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        start.cost = 0;
        start.parent = null;
        pq.offer(start);

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

}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

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

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

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

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

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

    }

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

        if(start==null){
            return;
        }

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

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

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

        }
    }
}


import java.util.*;

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    public static void dijkstra(int cities, HashMap<Integer, Node> graph, Node start) {
        PriorityQueue<Node> pq = new PriorityQueue<>(cities, new Comparator<Node>() {
            public int compare(Node o1, Node o2) {
                return o1.cost - o2.cost;
            }
        });
        start.cost = 0;
        start.parent = null;
        pq.offer(start);

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

}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

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

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

            if (!list.contains(1)) {
                System.out.println("-1");
            } else {
                Collections.reverse(list);
                StringBuilder s = new StringBuilder();
                for (int g : list) {
                    s.append(g);
                    s.append(" ");
                }
                System.out.println(s.toString());
            }
        }

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

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

    }

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

        if(start==null){
            return;
        }

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

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

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

        }
    }
}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

    }

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

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

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

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

}


import java.util.*;

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

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

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

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

    public static class Edge {
        int length;
        Node dest;

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

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

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

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

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

        Node end = graph.get(nodes);

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

            if (!list.contains(1)) {
                System.out.println("-1");
            } else {
                Collections.reverse(list);
                StringBuilder s = new StringBuilder();
                for (int g : list) {
                    s.append(g);
                    s.append(" ");
                }
                System.out.println(s.toString());
            }
        }

    }

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

        if(start==null){
            return;
        }

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

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

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

        }
    }
}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class DijkstraArrayList {

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

		//Scanner sc = new Scanner(System.in);
		//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
		//		long endTime = System.nanoTime();
		//		long elapsedTime = endTime - startTime;
		//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc) throws IOException{

		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();
		Node[] nodes = new Node[numNodes + 1];

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(nodes[first] == null){
				nodes[first] = firstNode;
			}
			nodes[first].neighbours.add(new Edge(cost, secondNode));
			if(nodes[second] == null){
				nodes[second] = secondNode;
			}
			nodes[second].neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes[1] == null || nodes[numNodes] == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes[1].prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes[1], numNodes);

			}
		}

	}

	private static void dijkstra(int numNodes, Node[] nodes){

		Node startingNode = nodes[numNodes];
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != 1){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					if(!e.dest.visited){
						Node neighbour = nodes[e.dest.value];
						if(currentPath.costFromStart + cost < neighbour.costFromStart){
							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}
				}
			} else {
				reached = true;
			}
		}
	}

	private static Path getPath(Node[] nodes, int dest) {
		Path result = new Path();
		Node end = nodes[dest];
		result.costFromStart = end.costFromStart;
		return result;
	}

	private static void printPath(Node dest, int numNodes) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		//OutputStream out = new BufferedOutputStream ( System.out );
		Node temp = dest;
		//StringBuilder sb = new StringBuilder(numNodes);
		while(temp != null){
			//System.out.print(temp.value + " ");
			 out.println(temp.value);
			 out.println(' ');

			//out.write((temp.value + " ").getBytes());
			temp = temp.prev;
		}
		out.close();
		//System.out.println(sb);
		//out.flush();
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

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

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

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

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

	}

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc) throws IOException{

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(1).prev == null){
				System.out.println("-1");
			} else {

				//printPath(nodes.get(numNodes));
				printPath(nodes.get(1));

			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(numNodes);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != 1){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							currentNode.next = neighbour; // WRONG!
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}

				}

			} else {
				reached = true;
				toVisit = new PriorityQueue<>();
				//nodes.clear();
			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
//		if(end.costFromStart != Integer.MAX_VALUE)
//			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path

	private static void printPath(Node dest) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		//OutputStream out = new BufferedOutputStream ( System.out );
		Node temp = dest;
		while(temp != null){
			//System.out.print(temp.value + " ");
			out.print(temp.value + " ");
			//out.write((temp.value + " ").getBytes());
			temp = temp.prev;
		}
		out.flush();
//		Stack<Node> stack = new Stack<Node>();
//		while(temp != null){
//			stack.add(temp);
//			temp = temp.prev;
//		}
//		while(!stack.isEmpty()){
//			System.out.print(stack.pop().value + " ");
//		}
		//
//		Node temp = dest;
//		while(temp.prev != null){
//			temp = temp.prev;
//		}
//		while(temp != null){
//			System.out.print(temp.value + " ");
//			temp = temp.next;
//		}
//		if(dest.prev != null) {
//			printPath(dest.prev);
//		}
//		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		Node next = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
public class Dijkstra {

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

		Scanner sc = new Scanner(System.in);

		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(Scanner sc){

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		//		for(Integer i: nodes.keySet()){
		//			Node n = nodes.get(i);
		//
		//			System.out.println("node: " + n.value + " is connected to: ");
		//			for(Edge e : n.neighbours){
		//				System.out.println(e.dest.value + " " + "cost to reach it is: " + e.cost);
		//			}
		//			System.out.println();
		//			System.out.println("----------------------------");
		//
		//
		//		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(numNodes).prev != null){
				printPath(nodes.get(numNodes));
			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(1);
		//PriorityQueue<Node> toVisit = new PriorityQueue<>();
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		//toVisit.add(startingNode);
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean goalSearched = false;
		//int minCost = Integer.MAX_VALUE;

		while(!toVisit.isEmpty()){
			//Node current = toVisit.remove();
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;
			//System.out.println("dequeue: " + current.value + " and cost from start: " + current.costFromStart);
			for(Edge e : currentNode.neighbours){
				int cost = e.cost;
				Node neighbour = nodes.get(e.dest.value);
				//System.out.println("attemping to visit neighbor: " + destination.value + " its cost from start is : " + destination.costFromStart);
				if(!e.visited){
					if(currentPath.costFromStart + cost < neighbour.costFromStart){

						neighbour.costFromStart = currentNode.costFromStart + cost;
						neighbour.prev = currentNode;

						if(neighbour.value == numNodes){
							//								if(minCost > destination.costFromStart){
							//									minCost = destination.costFromStart;
							//									//System.out.println("got it: " + destination.costFromStart);
							//								}
							//System.out.println("remaining in the queue: " + toVisit.size());
							//toVisit = new PriorityQueue<>();
							//System.out.println("got it: " + neighbour.value + " and cost from start: " + neighbour.costFromStart);
							//System.out.println("cost: " + neighbour.costFromStart);

						} else {

						}
						Path nextSpot = getPath(nodes, neighbour.value);
						nextSpot.dest = neighbour;
						toVisit.add(nextSpot);
						e.visited = true;
					}
				}

			}

		}

		//		//System.out.println("min cost: " + minCost);
		//		Node goal = nodes.get(5);
		//		//System.out.println("cost from start: " + goal.costFromStart);
		//		Node temp = nodes.get(5);
		//		Stack<Integer> thePath = new Stack<>();
		//		while(temp != null){
		//			//System.out.println("val: " + temp.value);
		//			thePath.push(temp.value);
		//			temp = temp.prev;
		//		}
		//
		//		while(!thePath.isEmpty()){
		//			System.out.print(thePath.pop() + " ");
		//		}

	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
		if(end.costFromStart != Integer.MAX_VALUE)
			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path
	private static void printPath(Node dest) {
		if(dest.prev != null) {
			printPath(dest.prev);
		}
		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		List<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new ArrayList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.add(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node implements Comparable<Node> {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}

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

	}

	private static class Edge {
		int cost;
		Node dest;
		boolean visited = false;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class DijkstraArrayList {

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

		//Scanner sc = new Scanner(System.in);
		//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
		//		long endTime = System.nanoTime();
		//		long elapsedTime = endTime - startTime;
		//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc) throws IOException{

		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();
		Node[] nodes = new Node[numNodes + 1];

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(nodes[first] == null){
				nodes[first] = firstNode;
			}
			nodes[first].neighbours.add(new Edge(cost, secondNode));
			if(nodes[second] == null){
				nodes[second] = secondNode;
			}
			nodes[second].neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes[1] == null || nodes[numNodes] == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes[1].prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes[1], numNodes);

			}
		}

	}

	private static void dijkstra(int numNodes, Node[] nodes){

		Node startingNode = nodes[numNodes];
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != 1){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					if(!e.dest.visited){
						Node neighbour = nodes[e.dest.value];
						if(currentPath.costFromStart + cost < neighbour.costFromStart){
							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}
				}
			} else {
				reached = true;
			}
		}
	}

	private static Path getPath(Node[] nodes, int dest) {
		Path result = new Path();
		Node end = nodes[dest];
		result.costFromStart = end.costFromStart;
		return result;
	}

	private static void printPath(Node dest, int numNodes) throws IOException {
		//PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		//OutputStream out = new BufferedOutputStream ( System.out );
		Node temp = dest;
		StringBuilder sb = new StringBuilder(numNodes);
		while(temp != null){
			//System.out.print(temp.value + " ");

			sb.append(temp.value);
			sb.append(' ');
			//out.write((temp.value + " ").getBytes());
			temp = temp.prev;
		}
		System.out.println(sb);
		//out.flush();
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

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

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

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

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

	}

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class DijkstraArrayList {

	// need to find from 1 to n
	// instead i did n to 1 for simplicity of printing out answer
	public static void main(String[] args) throws IOException{

		//Scanner sc = new Scanner(System.in);
		//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
		//		long endTime = System.nanoTime();
		//		long elapsedTime = endTime - startTime;
		//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc) throws IOException{

		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();
		Node[] nodes = new Node[numNodes + 1];

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(nodes[first] == null){
				nodes[first] = firstNode;
			}
			nodes[first].neighbours.add(new Edge(cost, secondNode));
			if(nodes[second] == null){
				nodes[second] = secondNode;
			}
			nodes[second].neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes[1] == null || nodes[numNodes] == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes[1].prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes[1], numNodes);

			}
		}

	}

	private static void dijkstra(int numNodes, Node[] nodes){

		Node startingNode = nodes[numNodes];
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != 1){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					if(!e.dest.visited){
						Node neighbour = nodes[e.dest.value];
						if(currentPath.costFromStart + cost < neighbour.costFromStart){
							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}
				}
			} else {
				reached = true;
			}
		}
	}

	private static Path getPath(Node[] nodes, int dest) {
		Path result = new Path();
		Node end = nodes[dest];
		result.costFromStart = end.costFromStart;
		return result;
	}

	private static void printPath(Node dest, int numNodes) throws IOException {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		Node temp = dest;
		while(temp != null){
			out.println(temp.value);
			temp = temp.prev;
		}
		out.close();
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;

		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

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

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

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

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

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

	}

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc){

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(numNodes).prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes.get(numNodes));
			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(1);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != numNodes){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
							e.visited = true;
						}
					}

				}

			} else {
				reached = true;
			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
		if(end.costFromStart != Integer.MAX_VALUE)
			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path
	private static void printPath(Node dest) {
		if(dest.prev != null) {
			printPath(dest.prev);
		}
		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		List<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new ArrayList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.add(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node implements Comparable<Node> {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}

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

	}

	private static class Edge {
		int cost;
		Node dest;
		boolean visited = false;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc){

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(numNodes).prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes.get(numNodes));
			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(1);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;

		while(!toVisit.isEmpty()){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != numNodes){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
							e.visited = true;
						}
					}

				}

			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
		if(end.costFromStart != Integer.MAX_VALUE)
			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path
	private static void printPath(Node dest) {
		if(dest.prev != null) {
			printPath(dest.prev);
		}
		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		List<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new ArrayList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.add(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node implements Comparable<Node> {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}

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

	}

	private static class Edge {
		int cost;
		Node dest;
		boolean visited = false;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}


import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc) throws IOException{

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(1).prev == null){
				System.out.println("-1");
			} else {

				//printPath(nodes.get(numNodes));
				printPath(nodes.get(1));

			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(numNodes);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != 1){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							currentNode.next = neighbour; // WRONG!
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}

				}

			} else {
				reached = true;
				toVisit = new PriorityQueue<>();
				//nodes.clear();
			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
//		if(end.costFromStart != Integer.MAX_VALUE)
//			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path

	private static void printPath(Node dest) throws IOException {
		//PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		OutputStream out = new BufferedOutputStream ( System.out );
		Node temp = dest;
		while(temp != null){
			//System.out.print(temp.value + " ");
			//out.println(temp.value + " ");
			out.write((temp.value + " ").getBytes());
			temp = temp.prev;
		}
		out.flush();
//		Stack<Node> stack = new Stack<Node>();
//		while(temp != null){
//			stack.add(temp);
//			temp = temp.prev;
//		}
//		while(!stack.isEmpty()){
//			System.out.print(stack.pop().value + " ");
//		}
		//
//		Node temp = dest;
//		while(temp.prev != null){
//			temp = temp.prev;
//		}
//		while(temp != null){
//			System.out.print(temp.value + " ");
//			temp = temp.next;
//		}
//		if(dest.prev != null) {
//			printPath(dest.prev);
//		}
//		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		Node next = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}


import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;

public class DijkstraAlt {

	static List<Node> nodes;

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int n = 0;
		int m = 0;
		n = sc.nextInt();
		m = sc.nextInt();

		nodes = new ArrayList<Node>();
		HashSet<Node> setNodes = new HashSet<Node>();
		for (int i = 0; i < n; i++) {
			nodes.add(new Node(i));
		}

		for (int i = 0; i < m; i++) {
			int source = sc.nextInt() - 1;
			int destination = sc.nextInt() - 1;
			int weight = sc.nextInt();
			nodes.get(source).addNeighbour(nodes.get(destination), weight);
			nodes.get(destination).addNeighbour(nodes.get(source), weight);
		}

		nodes.get(0).pathDistance = 0;
		PriorityQueue<Node> pq = new PriorityQueue<>(1, new Comparator<Node>() {
			@Override
			public int compare(Node n1, Node n2) {
				return n1.pathDistance - n2.pathDistance;
			}
		});

		// Dijkstra's algorithm
		pq.add(nodes.get(0));
		while(!pq.isEmpty()) {
			Node node = pq.poll();
			if (setNodes.contains(node)) continue;
			setNodes.add(node);
			for (Node neighbour : node.neighbours.keySet()) {
				// Evaluate shortest paths
				int distance = node.neighbours.get(neighbour);
				if (!setNodes.contains(neighbour) && neighbour.pathDistance > node.pathDistance + distance) {
					neighbour.pathDistance = node.pathDistance + distance;
					neighbour.previous = node;
					pq.add(neighbour);
				}
			}
		}

		Node currentNode = nodes.get(n - 1);
		if (currentNode.previous == null) {
			System.out.println("-1");
		}
		else {
			StringBuilder sb = new StringBuilder();
			StringBuilder sb2;
			while (currentNode != null) {
				sb2 = new StringBuilder(String.valueOf(currentNode.number + 1)).append(" ");
				sb2.reverse();
				sb.append(sb2);
				currentNode = currentNode.previous;
			}
			System.out.println(sb.reverse().toString());
		}
	}

	public static class Node {
		public Map<Node, Integer> neighbours;
		public Node previous;
		public int pathDistance;
		public int number;
		public Node(int number) {
			neighbours = new HashMap<>();
			pathDistance = Integer.MAX_VALUE;
			this.number = number;
		}

		void addNeighbour(Node destination, int weight) {
			if (neighbours.containsKey(destination) && neighbours.get(destination) <= weight) {
				return;
			}
			neighbours.put(destination, weight);
		}
	}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
		long endTime = System.nanoTime();
		long elapsedTime = endTime - startTime;
		double seconds = elapsedTime / 1.0E09;

		System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc){

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(numNodes).prev == null){
				System.out.println("-1");
			} else {
				printPath(nodes.get(numNodes));
			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(1);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != numNodes){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
							e.visited = true;
						}
					}

				}

			} else {
				reached = true;
			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
		if(end.costFromStart != Integer.MAX_VALUE)
			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
		if(end.prev != null)
			getPath(result, end.prev);
		result.add(end);
	}

	// helper to print path
	private static void printPath(Node dest) {
		if(dest.prev != null) {
			printPath(dest.prev);
		}
		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		List<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new ArrayList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.add(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node implements Comparable<Node> {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}

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

	}

	private static class Edge {
		int cost;
		Node dest;
		boolean visited = false;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
public class Dijkstra {

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

		//Scanner sc = new Scanner(System.in);
//		long startTime = System.nanoTime();
		MyScanner sc = new MyScanner();
		getShortestPath(sc);
//		long endTime = System.nanoTime();
//		long elapsedTime = endTime - startTime;
//		double seconds = elapsedTime / 1.0E09;

		//System.out.println("Total execution time: " + (seconds) );

	}

	private static void getShortestPath(MyScanner sc){

		HashMap<Integer, Node> nodes = new HashMap<>();
		int numNodes = sc.nextInt();
		int numEdges = sc.nextInt();

		for(int i = 0; i < numEdges; i++){
			int first = sc.nextInt();
			int second = sc.nextInt();
			int cost = sc.nextInt();
			Node firstNode = new Node(first);
			Node secondNode = new Node(second);
			if(!nodes.containsKey(first)){
				nodes.put(first, firstNode);
			}
			nodes.get(first).neighbours.add(new Edge(cost, secondNode));
			if(!nodes.containsKey(second)){
				nodes.put(second, secondNode);
			}
			nodes.get(second).neighbours.add(new Edge(cost, firstNode));

		}

		if(nodes.get(1) == null || nodes.get(numNodes) == null){
			System.out.println("-1");
		} else {
			dijkstra(numNodes, nodes);
			if(nodes.get(numNodes).prev == null){
				System.out.println("-1");
			} else {

				printPath(nodes.get(numNodes));
			}
		}

	}

	private static void dijkstra(int numNodes, HashMap<Integer, Node> nodes){

		Node startingNode = nodes.get(1);
		PriorityQueue<Path> toVisit = new PriorityQueue<>();
		Path startingPath = getPath(nodes, startingNode.value);
		startingPath.dest = startingNode;
		toVisit.add(startingPath);
		startingNode.costFromStart = 0;
		boolean reached = false;
		while(!toVisit.isEmpty() && !reached){
			Path currentPath = toVisit.remove();
			Node currentNode = currentPath.dest;
			currentNode.visited = true;

			if(currentNode.value != numNodes){
				for(Edge e : currentNode.neighbours){
					int cost = e.cost;
					Node neighbour = nodes.get(e.dest.value);
					if(!e.dest.visited){
						if(currentPath.costFromStart + cost < neighbour.costFromStart){

							neighbour.costFromStart = currentNode.costFromStart + cost;
							neighbour.prev = currentNode;
							currentNode.next = neighbour; // WRONG!
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}

				}

			} else {
				reached = true;
				toVisit = new PriorityQueue<>();
				//nodes.clear();
			}

		}
	}

	private static Path getPath(HashMap<Integer, Node> nodes, int dest) {
		Path result = new Path();
		Node end = nodes.get(dest);
		result.costFromStart = end.costFromStart;
		if(end.costFromStart != Integer.MAX_VALUE)
			getPath(result, end);
		return result;
	}

	// helper to get path
	private static void getPath(Path result, Node end) {
//		if(end.prev != null)
//			getPath(result, end.prev);
//		result.add(end);
		Node temp = end;
		while(temp != null){
			result.add(temp);
			temp = temp.prev;
		}
	}

	// helper to print path
	private static void printPath(Node dest) {
		Node temp = dest;
		Stack<Node> stack = new Stack<Node>();
		while(temp != null){
			stack.add(temp);
			temp = temp.prev;
		}
		while(!stack.isEmpty()){
			System.out.print(stack.pop().value + " ");
		}
		//
//		Node temp = dest;
//		while(temp.prev != null){
//			temp = temp.prev;
//		}
//		while(temp != null){
//			System.out.print(temp.value + " ");
//			temp = temp.next;
//		}
//		if(dest.prev != null) {
//			printPath(dest.prev);
//		}
//		System.out.print(dest.value + " ");
	}

	public static class Path implements Comparable<Path>{

		int costFromStart;
		LinkedList<Node> path;
		int number = 0;
		Node dest;
		public Path(){
			dest = new Node(-1);
			costFromStart = 0;
			path = new LinkedList<>();
		}

		public int numberNodes(){
			return path.size();
		}
		public void add(Node v) {
			path.addFirst(v);
		}

		@Override
		public int compareTo(Path other) {
			return costFromStart < other.costFromStart ? -1 : costFromStart > other.costFromStart ? 1 :0;
		}

	}

	private static class Node {

		int value;
		ArrayList<Edge> neighbours;
		int costFromStart = Integer.MAX_VALUE;
		Node prev = null;
		Node next = null;
		boolean visited;

		public Node(int v){
			value = v;
			neighbours = new ArrayList<Edge>();
			visited = false;
		}
	}

	private static class Edge {
		int cost;
		Node dest;

		public Edge(int cost, Node dest){
			this.cost = cost;
			this.dest = new Node(dest.value);

		}
	}

	  public static class MyScanner {
	      BufferedReader br;
	      StringTokenizer st;

	      public MyScanner() {
	         br = new BufferedReader(new InputStreamReader(System.in));
	      }

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

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

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

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

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

	   }

}