import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

//	public void setCost(int num)
//	{
//		cost += num;
//	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
//		for(int i = 1; i <= numV; i++)
//		{
//			List<Djikstra> nbrs = new ArrayList<Djikstra>();
//			graph.put(i, nbrs);
//		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			//if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				List<Djikstra> list = graph.get(v1);
				if(list != null)
					list.add(new Djikstra(v2, weight, null));
				else
				{
					List<Djikstra> nbrs = new ArrayList<Djikstra>();
					nbrs.add(new Djikstra(v2, weight, null));
					graph.put(v1, nbrs);
				}
			}

			//if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				List<Djikstra> list = graph.get(v2);
				if(list != null)
					list.add(new Djikstra(v1, weight, null));
				else
				{
					List<Djikstra> nbrs = new ArrayList<Djikstra>();
					nbrs.add(new Djikstra(v1, weight, null));
					graph.put(v2, nbrs);
				}
			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[goal];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			for(int i = 0; i < neighbors.size(); i++)
			{
				Djikstra nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i > 0; i-=2)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

			if(numE > 1)
			{
				v1 = scan.nextInt();
				v2 = scan.nextInt();
				weight = scan.nextInt();

				graph.get(v1).add(new Djikstra(v2, weight, null));
				graph.get(v2).add(new Djikstra(v1, weight, null));
			}

		}

		if(numE % 2 == 1)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i-=2)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
				if(i-1 >= 0)
				{
					nbr = neighbors.get(i-1);
					if(visited[nbr.getNodeNumber()-1] == false)
					{
						int cost = node.getCost() + nbr.getCost();
						Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
						queue.add(nextNode);
					}
				}

			}

			if(size % 2 == 1)
			{
				Djikstra nbr = neighbors.get(size-1);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i >= 0; i--)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
//			if(visited[nodeNum-1])
//				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i--)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;
import java.util.PriorityQueue;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	//private boolean visited;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//visited = false;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

//	public boolean checkVisited()
//	{
//		return visited;
//	}
//
//	public void visit(int num)
//	{
//		cost += num;
//	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		int[][] graph = new int[numV][numV];
		for(int i = 0; i < numE; i++)
		{
			int edgeA = scan.nextInt();
			int edgeB = scan.nextInt();
			int weight = scan.nextInt();
			if(graph[edgeA-1][edgeB-1] == 0)
			{
				graph[edgeA-1][edgeB-1] = weight;
				graph[edgeB-1][edgeA-1] = weight;
			}

			else
			{
				if(weight < graph[edgeA-1][edgeB-1])
				{
					graph[edgeA-1][edgeB-1] = weight;
					graph[edgeB-1][edgeA-1] = weight;
				}

			}
		}

//		for(int r = 0; r < graph.length; r++)
//		{
//			for(int c = 0; c <graph[r].length; c++)
//			{
//				System.out.print(graph[r][c] + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(int[][] graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.length];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			for(int i = 0; i < graph[node.getNodeNumber()-1].length; i++)
			{
				if(graph[node.getNodeNumber()-1][i] != 0 && i != node.getNodeNumber()-1)
				{
					//System.out.println(i+1);
					if(visited[i] == false)
					{
						//System.out.println("VISITED IS FALSE");
						int cost = node.getCost() + graph[node.getNodeNumber()-1][i];
						Djikstra nextNode = new Djikstra(i+1, cost, node);
						queue.add(nextNode);
					}

				}

			}
		}

		System.out.println(-1);

	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

//	public void setCost(int num)
//	{
//		cost += num;
//	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
//		for(int i = 1; i <= numV; i++)
//		{
//			List<Djikstra> nbrs = new ArrayList<Djikstra>();
//			graph.put(i, nbrs);
//		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			//if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				List<Djikstra> list = graph.get(v1);
				if(list != null)
					list.add(new Djikstra(v2, weight, null));
				else
				{
					List<Djikstra> nbrs = new ArrayList<Djikstra>();
					nbrs.add(new Djikstra(v2, weight, null));
					graph.put(v1, nbrs);
				}
			}

			//if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				List<Djikstra> list = graph.get(v2);
				if(list != null)
					list.add(new Djikstra(v1, weight, null));
				else
				{
					List<Djikstra> nbrs = new ArrayList<Djikstra>();
					nbrs.add(new Djikstra(v1, weight, null));
					graph.put(v2, nbrs);
				}
			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[goal];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			if(neighbors == null)
				continue;
			for(int i = 0; i < neighbors.size(); i++)
			{
				Djikstra nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	//public List<Djikstra> neighbors;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//neighbors = new ArrayList<Djikstra>();
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		//List<Djikstra> graph = new ArrayList<Integer>()
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i >= 0; i--)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				StringBuilder output = new StringBuilder("" + nodeNum);
				while(prev != null)
				{
					output.insert(0, prev.getNodeNumber() + " ");
					prev = prev.getPrev();
				}
				System.out.println(output);
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i--)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	private List<Djikstra> neighbors;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i > 0; i--)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i-=2)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		//Map<Integer, List<Integer[]>> graph = new HashMap<Integer, List<Integer[]>>();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = 1; i <= numV; i++)
		{
			//List<Integer[]> nbrs = new ArrayList<Integer[]>();
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			//if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				Integer[] arr = {v2, weight};
				graph.get(v1).add(new Djikstra(v2, weight, null));
			}

			//if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				Integer[] arr = {v1, weight};
				graph.get(v2).add(new Djikstra(v1, weight, null));
			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			//List<Integer[]> neighbors = graph.get(node.getNodeNumber());
			List<Djikstra> neighbors = graph.get(node.getNodeNumber());
			for(int i = 0; i < neighbors.size(); i++)
			{
				Djikstra nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					//if(queue.isEmpty() || (!queue.isEmpty() && cost < queue.peek().getCost()))
						queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = 1; i <= numV; i++)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i > 0; i-=2)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

			if(numE > 1)
			{
				v1 = scan.nextInt();
				v2 = scan.nextInt();
				weight = scan.nextInt();

				graph.get(v1).add(new Djikstra(v2, weight, null));
				graph.get(v2).add(new Djikstra(v1, weight, null));
			}

		}

		if(numE % 2 == 1)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			for(int i = neighbors.size()-1; i >= 0; i--)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;
import java.util.PriorityQueue;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		int[][] graph = new int[numV][numV];
		for(int i = 0; i < numE; i++)
		{
			int edgeA = scan.nextInt();
			int edgeB = scan.nextInt();
			int weight = scan.nextInt();
			graph[edgeA-1][edgeB-1] = weight;
		}

//		for(int r = 0; r < graph.length; r++)
//		{
//			for(int c = 0; c <graph[r].length; c++)
//			{
//				System.out.print(graph[r][c] + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
		//System.out.println("gyugyuguy");
	}

	private static void djikstra(int[][] graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.length];
		//System.out.println(Arrays.toString(visited));

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output);
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			for(int i = 0; i < graph[node.getNodeNumber()-1].length; i++)
			{
				//System.out.println(graph[node.getNodeNumber()-1][i]);
				if(graph[node.getNodeNumber()-1][i] != 0 && i != node.getNodeNumber()-1)
				{
					//System.out.println(visited[i]);
					if(visited[i] == false)
					{
						//System.out.println("jiojoijoi");
						int cost = node.getCost() + graph[node.getNodeNumber()-1][i];
						Djikstra nextNode = new Djikstra(i+1, cost, node);
						//System.out.println(nextNode.getNodeNumber());
						queue.add(nextNode);
					}

				}

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

	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		//Map<Integer, List<Integer[]>> graph = new HashMap<Integer, List<Integer[]>>();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = 1; i <= numV; i++)
		{
			//List<Integer[]> nbrs = new ArrayList<Integer[]>();
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			//if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				//Integer[] arr = {v2, weight};
				graph.get(v1).add(new Djikstra(v2, weight, null));
			}

			//if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				//Integer[] arr = {v1, weight};
				graph.get(v2).add(new Djikstra(v1, weight, null));
			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			//List<Integer[]> neighbors = graph.get(node.getNodeNumber());
			List<Djikstra> neighbors = graph.get(nodeNum);
			for(int i = 0; i < neighbors.size(); i++)
			{
				Djikstra nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	//private boolean visited;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//visited = false;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

//	public boolean checkVisited()
//	{
//		return visited;
//	}
//
//	public void visit(int num)
//	{
//		cost += num;
//	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		//int[][] graph = new int[numV][numV];
		Map<Integer, List<Integer[]>> graph = new HashMap<Integer, List<Integer[]>>();
		for(int i = 1; i <= numV; i++)
		{
			List<Integer[]> nbrs = new ArrayList<Integer[]>();
			graph.put(i, nbrs);
			//System.out.println(i);
		}

		for(int i = 0; i < numE; i++)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			if(!isVertexANeighbor(graph, v1, v2, weight))
			{
				Integer[] arr = {v2, weight};
				graph.get(v1).add(arr);
			}

			if(!isVertexANeighbor(graph, v2, v1, weight))
			{
				Integer[] arr = {v1, weight};
				graph.get(v2).add(arr);
			}

//			if(graph[edgeA-1][edgeB-1] == 0)
//			{
//				graph[edgeA-1][edgeB-1] = weight;
//				graph[edgeB-1][edgeA-1] = weight;
//			}
//
//			else
//			{
//				if(weight < graph[edgeA-1][edgeB-1])
//				{
//					graph[edgeA-1][edgeB-1] = weight;
//					graph[edgeB-1][edgeA-1] = weight;
//				}
//
//			}
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Integer[]> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(Arrays.toString(list.get(c)) + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Integer[]>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			//System.out.println(node.getNodeNumber());
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			List<Integer[]> neighbors = graph.get(node.getNodeNumber());
			for(int i = 0; i < neighbors.size(); i++)
			{
				Integer[] nbr = neighbors.get(i);
				//System.out.println(nbr[0]);
				if(visited[nbr[0]-1] == false)
				{
					//System.out.println("VISITED IS FALSE");
					int cost = node.getCost() + nbr[1];
					Djikstra nextNode = new Djikstra(nbr[0], cost, node);
					queue.add(nextNode);
				}

			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	public List<Djikstra> neighbors;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
		//neighbors = new ArrayList<Djikstra>();
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public String toString()
	{
		return "" + number;
	}

	public static void main (String[] args)
	{
//    	Stopwatch watch = new Stopwatch();
//    	watch.start();
		String s = "10 10 1 4 201 2 3 238 3 4 40 3 6 231 3 8 45 4 5 227 4 6 58 4 9 55 5 7 14 6 10 242";
		Scanner scan = new Scanner(s);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		//List<Djikstra> graph = new ArrayList<Integer>()
		for(int i = numV; i > 0; i--)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i >= 0; i--)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);

//		watch.stop();
//		System.out.println(watch);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(nodeNum == goal)
			{
				List<Djikstra> prevs = new ArrayList<Djikstra>();
				prevs.add(node);
				Djikstra prev = node.getPrev();
				//StringBuilder output = new StringBuilder("" + nodeNum);
				while(prev != null)
				{
					//output.insert(0, prev.getNodeNumber() + " ");
					prevs.add(prev);
					prev = prev.getPrev();
				}
				for(int i = prevs.size()-1; i >=0; i--)
					System.out.print(prevs.get(i) + " ");
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i--)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;
	// 10^6 * 10^5 = 10^11 = not fit in an integer

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		Map<Integer, List<Djikstra>> graph = new HashMap<Integer, List<Djikstra>>();
		for(int i = 1; i <= numV; i++)
		{
			List<Djikstra> nbrs = new ArrayList<Djikstra>();
			graph.put(i, nbrs);
		}

		for(int i = numE-1; i > 0; i-=2)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));

			if(numE > 1)
			{
				v1 = scan.nextInt();
				v2 = scan.nextInt();
				weight = scan.nextInt();

				graph.get(v1).add(new Djikstra(v2, weight, null));
				graph.get(v2).add(new Djikstra(v1, weight, null));
			}

		}

		if(numE % 2 == 1)
		{
			int v1 = scan.nextInt();
			int v2 = scan.nextInt();
			int weight = scan.nextInt();

			graph.get(v1).add(new Djikstra(v2, weight, null));
			graph.get(v2).add(new Djikstra(v1, weight, null));
		}

//		for(int i : graph.keySet())
//		{
//			System.out.print(i + ": ");
//			List<Djikstra> list = graph.get(i);
//			for(int c = 0; c < list.size(); c++)
//			{
//				System.out.print(list.get(c).getNodeNumber + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
	}

	private static void djikstra(Map<Integer, List<Djikstra>> graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.size()];

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			int nodeNum = node.getNodeNumber();
			if(visited[nodeNum-1])
				continue;
			if(nodeNum == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + nodeNum;
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output.trim());
				return;
			}

			visited[nodeNum-1] = true;
			List<Djikstra> neighbors = graph.get(nodeNum);
			int size = neighbors.size();
			for(int i = size-1; i >= 0; i-=2)
			{
				Djikstra nbr = neighbors.get(i);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
				if(i-1 >= 0)
				{
					nbr = neighbors.get(i-1);
					if(visited[nbr.getNodeNumber()-1] == false)
					{
						int cost = node.getCost() + nbr.getCost();
						Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
						queue.add(nextNode);
					}
				}

			}

			if(size % 2 == 1)
			{
				Djikstra nbr = neighbors.get(size-1);
				if(visited[nbr.getNodeNumber()-1] == false)
				{
					int cost = node.getCost() + nbr.getCost();
					Djikstra nextNode = new Djikstra(nbr.getNodeNumber(), cost, node);
					queue.add(nextNode);
				}
			}
		}

		System.out.println(-1);

	}

	private static boolean isVertexANeighbor(Map<Integer, List<Integer[]>> graph, int v1, int v2, int weight)
	{
		//System.out.println(v1);
		List<Integer[]> neighbors = graph.get(v1);
		if(neighbors == null)
			return false;
		for(int n = 0; n < neighbors.size(); n++)
		{
			Integer[] neighbor = neighbors.get(n);
			if(neighbor[0] == v2)
			{
				if(weight < neighbor[1])
				{
					neighbor[1] = weight;
					neighbors.set(n, neighbor);
					graph.put(v1, neighbors);
					return true;
				}

			}

		}

		return false;
	}

}


import java.util.*;
import java.util.PriorityQueue;

public class Djikstra implements Comparable<Djikstra>
{
	private int number;
	private int cost;
	private Djikstra prev;

	public Djikstra(int num, int c, Djikstra previous)
	{
		number = num;
		cost = c;
		prev = previous;
	}

	public int getNodeNumber()
	{
		return number;
	}

	public int getCost()
	{
		return cost;
	}

	public void setCost(int num)
	{
		cost += num;
	}

	public Djikstra getPrev()
	{
		return prev;
	}

	@Override
	public int compareTo(Djikstra o) {
		return this.cost - o.getCost();
	}

	public static void main (String[] args)
	{
		//String s = "5 6 1 2 2 2 5 5 2 3 4 1 4 1 4 3 3 3 5 1";
		Scanner scan = new Scanner(System.in);
		int numV = scan.nextInt();
		int numE = scan.nextInt();
		int[][] graph = new int[numV][numV];
		for(int i = 0; i < numE; i++)
		{
			int edgeA = scan.nextInt();
			int edgeB = scan.nextInt();
			int weight = scan.nextInt();
			if(graph[edgeA-1][edgeB-1] == 0)
				graph[edgeA-1][edgeB-1] = weight;
			else
			{
				if(weight < graph[edgeA-1][edgeB-1])
					graph[edgeA-1][edgeB-1] = weight;
			}
		}

//		for(int r = 0; r < graph.length; r++)
//		{
//			for(int c = 0; c <graph[r].length; c++)
//			{
//				System.out.print(graph[r][c] + " ");
//			}
//			System.out.println();
//		}
		djikstra(graph, numV);
		//System.out.println("gyugyuguy");
	}

	private static void djikstra(int[][] graph, int goal)
	{
		PriorityQueue<Djikstra> queue = new PriorityQueue<Djikstra>();
		boolean[] visited = new boolean[graph.length];
		//System.out.println(Arrays.toString(visited));

		Djikstra start = new Djikstra(1, 0, null);
		queue.add(start);

		while (!queue.isEmpty())
		{
			Djikstra node = queue.poll();
			if(visited[node.getNodeNumber()-1])
				continue;
			if(node.getNodeNumber() == goal)
			{
				Djikstra prev = node.getPrev();
				String output = "" + node.getNodeNumber();
				while(prev != null)
				{
					output = prev.getNodeNumber() + " " + output;
					prev = prev.getPrev();
				}
				System.out.println(output);
				return;
			}

			visited[node.getNodeNumber()-1] = true;
			for(int i = 0; i < graph[node.getNodeNumber()-1].length; i++)
			{
				//System.out.println(graph[node.getNodeNumber()-1][i]);
				if(graph[node.getNodeNumber()-1][i] != 0 && i != node.getNodeNumber()-1)
				{
					//System.out.println(visited[i]);
					if(visited[i] == false)
					{
						//System.out.println("jiojoijoi");
						int cost = node.getCost() + graph[node.getNodeNumber()-1][i];
						Djikstra nextNode = new Djikstra(i+1, cost, node);
						//System.out.println(nextNode.getNodeNumber());
						queue.add(nextNode);
					}

				}

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

	}

}


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

/**
 * The problem can be found here: http://www.spoj.com/problems/EZDIJKST/
 */
public class FindTheBugWeek2 {
    public static class Node implements Comparable<Node> {
        int id;
        List<Edge> neighbors;
        long cost;
        boolean visited;
        Node prev;

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        source.cost = 0;
        source.visited = true;
        pq.add(source);

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

            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.prev = current;
                    //nextNode.visited = true;
                    pq.add(nextNode);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);
        //final int numCases = scan.nextInt();

        //for (int caseNum = 0; caseNum < numCases; caseNum++) {
            // Read in input
            final int numVertices = scan.nextInt();
            final int numEdges = scan.nextInt();

            final List<Node> graph = new ArrayList<>(numVertices);
            for (int i = 0; i < numVertices; i++) {
                graph.add(new Node(i));
            }

            for (int i = 0; i < numEdges; i++) {
                // Convert to 0 indexing
                final int start = scan.nextInt() - 1;
                final int end = scan.nextInt() - 1;
                final int weight = scan.nextInt();

                final Node nodeStart = graph.get(start);
                final Node nodeEnd = graph.get(end);

                nodeStart.neighbors.add(new Edge(nodeEnd, weight));
                nodeEnd.neighbors.add(new Edge(nodeStart, weight));
            }

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

            // All of the costs will be saved in the node objects
            dijkstra(graph, graph.get(source));

            long ans = graph.get(sink).cost;
            if (ans == Long.MAX_VALUE) {
                System.out.println("-1");
            } else
            {
				Node prev = graph.get(sink).prev;
				StringBuilder output = new StringBuilder("" + (sink+1));
				while(prev != null)
				{
					output.insert(0, (prev.id+1) + " ");
					prev = prev.prev;
				}
				System.out.println(output);
                //System.out.println(ans);
            }
       // }
    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static int[] dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        int[] path = new int[graph.size()];
        Arrays.fill(path, 0);
        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 1;
        while (!pq.isEmpty()) {
            Node current = pq.remove();

            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;
                //System.out.println(nextNode.id+1);

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    path[count] = nextNode.id+1;
                }
                if (nextNode == destination) {
                    return path;
                }
            }
            count++;
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

            final Node nodeStart = graph.get(start);
            final Node nodeEnd = graph.get(end);

            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

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

        // All of the costs will be saved in the node objects
        int[] path = dijkstra(graph, graph.get(source), graph.get(sink));
        if (path == null) {
            System.out.print(-1);
        } else {
            path[0] = 1;
            for (int i = 0; i < numVertices; i++) {
                if (path[i] != 0)
                    System.out.print(path[i] + " ");
                else
                    break;
            }
        }
    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

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

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

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

        int nextInt()
        {
            return Integer.parseInt(next());
        }
        long nextLong()
        {
            return Long.parseLong(next());
        }
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (java.io.IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

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

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

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

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    path[count+1] = nextNode.id+1;
                    //System.out.println(nextNode.id+1);
                }
                if (nextNode == destination) {
                    path[count+1] = nextNode.id+1;
                    return path;
                }
            }
            count++;
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

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

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

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

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

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    //System.out.println(nextNode.id+1);
                }
                if (nextNode == destination) {
                    path[count+1] = nextNode.id+1;
                    return path;
                }
            }
            count++;
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

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

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

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

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

        int nextInt()
        {
            return Integer.parseInt(next());
        }
        long nextLong()
        {
            return Long.parseLong(next());
        }
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (java.io.IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

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

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

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

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

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

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

        int nextInt()
        {
            return Integer.parseInt(next());
        }
        long nextLong()
        {
            return Long.parseLong(next());
        }
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (java.io.IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

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

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static int[] dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();
        int[] path = new int[graph.size()];
        Arrays.fill(path, 0);
        source.cost = 0;
        source.visited = true;
        pq.add(source);
        int count = 1;
        while (!pq.isEmpty()) {
            Node current = pq.remove();

            for (Edge edge : current.neighbors) {
                Node nextNode = edge.dest;
                long newCost = current.cost + edge.weight;

                if (!nextNode.visited && newCost < nextNode.cost) {
                    nextNode.cost = newCost;
                    nextNode.visited = true;
                    pq.add(nextNode);
                    path[count] = nextNode.id+1;
                }
                if (nextNode == destination) {
                    return path;
                }
            }
            count++;
        }
        return null;
    }

    public static void main(String[] args) throws IOException {
        final Scanner scan = new Scanner(System.in);

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

            final Node nodeStart = graph.get(start);
            final Node nodeEnd = graph.get(end);

            nodeStart.neighbors.add(new Edge(nodeEnd, weight));
        }

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

        // All of the costs will be saved in the node objects
        int[] path = dijkstra(graph, graph.get(source), graph.get(sink));
        if (path == null) {
            System.out.print(-1);
        } else {
            path[0] = 1;
            for (int i = 0; i < numVertices-1; i++) {
                if (path[i] != 0)
                    System.out.print(path[i] + " ");
                else
                    break;
            }
        }
    }
}


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

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

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

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

    public static class Edge {
        Node dest;
        int weight;

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

    public static void dijkstra(List<Node> graph, Node source, Node destination) {
        PriorityQueue<Node> pq = new PriorityQueue<>();

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

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

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

        int nextInt()
        {
            return Integer.parseInt(next());
        }
        long nextLong()
        {
            return Long.parseLong(next());
        }
        double nextDouble()
        {
            return Double.parseDouble(next());
        }
        String nextLine()
        {
            String str = "";
            try
            {
                str = br.readLine();
            }
            catch (java.io.IOException e)
            {
                e.printStackTrace();
            }
            return str;
        }
    }

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

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

        final List<Node> graph = new ArrayList<>(numVertices);
        for (int i = 0; i < numVertices; i++) {
            graph.add(new Node(i));
        }

        for (int i = 0; i < numEdges; i++) {
            // Convert to 0 indexing
            final int start = scan.nextInt() - 1;
            final int end = scan.nextInt() - 1;
            final int weight = scan.nextInt();

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

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

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

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

    }
}