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

		dijkstra(numNodes, nodes);
		if(nodes.get(numNodes) == null || 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<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 + " ");
			//out.write((temp.value + " ").getBytes());
			out.println(temp.value);
			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.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.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);
	}

	// 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;
		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 {

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

		dijkstra(numNodes, nodes);
		if(nodes.get(1) == null || nodes.get(numNodes) == null || 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<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.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);
						}
					}

				}

			} 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) {
		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 {

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

	// 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;
		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 {

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

						} 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 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(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;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}
				}
			} 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;
		return result;
	}

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

	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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
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 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(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) {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		Node temp = dest;
		while(temp != null){
			//System.out.print(temp.value + " ");
			out.println(temp.value + " ");
			temp = temp.prev;
		}
//		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.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 + " ");
			System.out.print(temp.value);
			System.out.print(' ');

			//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.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.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);
	}

	// 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;
		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 {

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

//		long startTime = System.nanoTime();
		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("----------------------------");
		//
		//
		//		}

		dijkstra(numNodes, nodes);
		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 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(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;
							Path nextSpot = getPath(nodes, neighbour.value);
							nextSpot.dest = neighbour;
							toVisit.add(nextSpot);
						}
					}
				}
			} 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;
		return result;
	}

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

	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.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
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 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(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) {
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		Node temp = dest;
		while(temp != null){
			System.out.print(temp.value + " ");
			//out.println(temp.value + " ");
			temp = temp.prev;
		}
//		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.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(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) {
		Node temp = dest;
		while(temp != null){
			System.out.print(temp.value + " ");
			temp = temp.prev;
		}
//		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.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.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;
							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) {
//		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;
		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 {

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

	   }

}