using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var tokens = Console.ReadLine().Split();
            if (tokens != null)
            {
                var vertices = int.Parse(tokens[0]);
                var destination = vertices;
                var edges = int.Parse(tokens[1]);
                var listConnectedVertices = new List<Edge>[vertices + 1];
                var distanceToTravels = new int[vertices + 1];
                Array.Fill(distanceToTravels, int.MaxValue);
                for (var i = 0; i < listConnectedVertices.Length; i++)
                {
                    listConnectedVertices[i] = new List<Edge>();
                }
                var priorityQueue = new PriorityQueue<Edge>();
                for (var i = 0; i < edges; i++)
                {
                    tokens = Console.ReadLine().Split();
                    if (tokens == null) continue;
                    var edge = new Edge(int.Parse(tokens[0]), int.Parse(tokens[1]), int.Parse(tokens[2]),
                        new List<int> {int.Parse(tokens[0]), int.Parse(tokens[1])});

                    listConnectedVertices[edge.From].Add(edge);
                }

                foreach (var edge in listConnectedVertices[1])
                {
                    priorityQueue.Enqueue(edge);
                }

                var current = priorityQueue.Dequeue();
                while (priorityQueue.Count() > 0 && current.To != destination)
                {
                    for (var i = 0; i < listConnectedVertices[current.To].Count; i++)
                    {
                        var nextEdge = listConnectedVertices[current.To][i];
                        var newEdge = new Edge (current.From, nextEdge.To, current.Distance + nextEdge.Distance, current.Path);
                        newEdge.Path.Add(nextEdge.To);
                        if (distanceToTravels[nextEdge.To] <= newEdge.Distance) continue;
                        priorityQueue.Enqueue(newEdge);
                        distanceToTravels[nextEdge.To] = newEdge.Distance;
                    }

                    current = priorityQueue.Dequeue();
                }

                if (current.To != destination)
                {
                    Console.WriteLine("-1");
                    return;
                }
                var result = string.Join(" ", current.Path);
                Console.WriteLine(result);
            }
        }
    }

    public class Edge : IComparable<Edge>
    {
        private readonly int _from;
        public int From { get; set; }
        public int To { get; set; }
        public int Distance { get; set; }
        public List<int> Path { get; set; }

        public Edge(int from, int to, int distance, List<int> path)
        {
            _from = @from;
            To = to;
            Distance = distance;
            Path = path;
        }
        public int CompareTo(Edge other)
        {
            return Distance.CompareTo(other.Distance);
        }
    }
    public class PriorityQueue<T> where T : IComparable<T>
    {
        private readonly List<T> _items = new List<T>();
        private int _count;

        public void Enqueue(T item)
        {
            if (_count == _items.Count)
                _items.Add(item);
            else
            {
                _items[_count] = item;
            }

            _count++;
            var childIndex = _count - 1;
            while (childIndex > 0)
            {
                var parentNodeIndex = (childIndex - 1) / 2;
                if (_items[parentNodeIndex].CompareTo(_items[childIndex]) <= 0)
                    break;
                var temp = _items[parentNodeIndex];
                _items[parentNodeIndex] = _items[childIndex];
                _items[childIndex] = temp;

                childIndex = parentNodeIndex;
            }
        }

        public T Dequeue()
        {
            if (_count == 0)
                return default;

            var firstItem = _items[0];
            var lastIndex = _count - 1;
            _items[0] = _items[lastIndex];
            _count--;

            lastIndex = _count - 1;
            var parentNodeIndex = 0;
            while (parentNodeIndex < lastIndex)
            {
                var leftNodeIndex = parentNodeIndex * 2 + 1;
                var rightNodeIndex = leftNodeIndex + 1;
                var childIndex = leftNodeIndex;
                if (leftNodeIndex > lastIndex)
                    break;
                if (rightNodeIndex <= lastIndex && _items[leftNodeIndex].CompareTo(_items[rightNodeIndex]) >= 0)
                    childIndex = rightNodeIndex;
                if (childIndex <= lastIndex && _items[childIndex].CompareTo(_items[parentNodeIndex]) > 0)
                    break;
                var temp = _items[parentNodeIndex];
                _items[parentNodeIndex] = _items[childIndex];
                _items[childIndex] = temp;

                parentNodeIndex = childIndex;
            }

            return firstItem;
        }

        public int Count()
        {
            return _count;
        }

        public bool CheckQueue()
        {
            if (_count == 0)
                return true;

            var lastIndex = _count - 1;

            for (var i = 0; i < _count; i++)
            {
                var leftIndex = i * 2 + 1;
                var rightIndex = leftIndex + 1;
                if (leftIndex <= lastIndex && _items[i].CompareTo(_items[leftIndex]) > 0)
                    return false;
                if (rightIndex <= lastIndex && _items[i].CompareTo(_items[rightIndex]) > 0)
                    return false;
            }

            return true;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
    public class Program
    {
        public static void Main()
        {
            var tokens = Console.ReadLine()?.Split();
            if (tokens != null)
            {
                var vertices = int.Parse(tokens[0]);
                var destination = vertices;
                var edges = int.Parse(tokens[1]);
                var listConnectedVertices = new List<Edge>[vertices + 1];
                var distanceToTravels = new int[vertices + 1];
                Array.Fill(distanceToTravels, int.MaxValue);
                for (var i = 0; i < listConnectedVertices.Length; i++)
                {
                    listConnectedVertices[i] = new List<Edge>();
                }
                var priorityQueue = new PriorityQueue<Edge>();
                for (var i = 0; i < edges; i++)
                {
                    tokens = Console.ReadLine()?.Split();
                    if (tokens == null) continue;
                    var edge = new Edge
                    {
                        From = int.Parse(tokens[0]),
                        To = int.Parse(tokens[1]),
                        Distance = int.Parse(tokens[2]),
                        Path = new List<int>{ int.Parse(tokens[0]) , int.Parse(tokens[1]) }
                    };

                    listConnectedVertices[edge.From].Add(edge);
                }

                foreach (var edge in listConnectedVertices[1])
                {
                    priorityQueue.Enqueue(edge);
                }

                var current = priorityQueue.Dequeue();
                while (priorityQueue.Count() > 0 && current.To != destination)
                {
                    for (var i = 0; i < listConnectedVertices[current.To].Count; i++)
                    {
                        var nextEdge = listConnectedVertices[current.To][i];
                        var newEdge = new Edge
                        {
                            From = current.From,
                            To = nextEdge.To,
                            Distance = current.Distance + nextEdge.Distance,
                            Path = current.Path
                        };
                        newEdge.Path.Add(nextEdge.To);
                        if (distanceToTravels[nextEdge.To] <= newEdge.Distance) continue;
                        priorityQueue.Enqueue(newEdge);
                        distanceToTravels[nextEdge.To] = newEdge.Distance;
                    }

                    current = priorityQueue.Dequeue();
                }

                if (current.To != destination)
                {
                    Console.WriteLine("-1");
                    return;
                }
                var result = string.Join(" ", current.Path);
                Console.WriteLine(result);
            }
        }
    }
    public class PriorityQueue<T> where T : IComparable<T>
    {
        private readonly List<T> _items = new List<T>();
        private int _count;

        public void Enqueue(T item)
        {
            if (_count == _items.Count)
                _items.Add(item);
            else
            {
                _items[_count] = item;
            }

            _count++;
            var childIndex = _count - 1;
            while (childIndex > 0)
            {
                var parentNodeIndex = (childIndex - 1) / 2;
                if (_items[parentNodeIndex].CompareTo(_items[childIndex]) <= 0)
                    break;
                var temp = _items[parentNodeIndex];
                _items[parentNodeIndex] = _items[childIndex];
                _items[childIndex] = temp;

                childIndex = parentNodeIndex;
            }
        }

        public T Dequeue()
        {
            if (_count == 0)
                return default;

            var firstItem = _items[0];
            var lastIndex = _count - 1;
            _items[0] = _items[lastIndex];
            _count--;

            lastIndex = _count - 1;
            var parentNodeIndex = 0;
            while (parentNodeIndex < lastIndex)
            {
                var leftNodeIndex = parentNodeIndex * 2 + 1;
                var rightNodeIndex = leftNodeIndex + 1;
                var childIndex = leftNodeIndex;
                if (leftNodeIndex > lastIndex)
                    break;
                if (rightNodeIndex <= lastIndex && _items[leftNodeIndex].CompareTo(_items[rightNodeIndex]) >= 0)
                    childIndex = rightNodeIndex;
                if (childIndex <= lastIndex && _items[childIndex].CompareTo(_items[parentNodeIndex]) > 0)
                    break;
                var temp = _items[parentNodeIndex];
                _items[parentNodeIndex] = _items[childIndex];
                _items[childIndex] = temp;

                parentNodeIndex = childIndex;
            }

            return firstItem;
        }

        public int Count()
        {
            return _count;
        }

        public bool CheckQueue()
        {
            if (_count == 0)
                return true;

            var lastIndex = _count - 1;

            for (var i = 0; i < _count; i++)
            {
                var leftIndex = i * 2 + 1;
                var rightIndex = leftIndex + 1;
                if (leftIndex <= lastIndex && _items[i].CompareTo(_items[leftIndex]) > 0)
                    return false;
                if (rightIndex <= lastIndex && _items[i].CompareTo(_items[rightIndex]) > 0)
                    return false;
            }

            return true;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using static System.Console;
using static System.Convert;
using static System.Math;
//using Debug;
//using static System.Globalization.CultureInfo;
//using Edge = Pair<long, int>;
using System.Text;
class Program
{
    static void Main(string[] args)
    {
        Solve();
        //WriteLine(Solve());
    }
    static int[] pa;
    static void Solve()
    {
        var nm = Input.ar;
        var dic = Enumerable.Range(0, nm[0]).Select(v => new Dictionary<int, Pair<long, int>>()).ToArray();
        while (nm[1]-- > 0)
        {
            var abw = Input.ar;
            abw[0]--;abw[1]--;
            if (abw[0] == abw[1]) continue;
            for (var j = 0; j < 2; j++)
                if (dic[abw[j]].ContainsKey(abw[1 - j]) && abw[2] < dic[abw[j]][abw[1 - j]].v1)
                    dic[abw[j]][abw[1 - j]] = new Pair<long, int>(abw[2], abw[1 - j]);
                else if (!dic[abw[j]].ContainsKey(abw[1 - j]))
                    dic[abw[j]][abw[1 - j]] = new Pair<long, int>(abw[2], abw[1 - j]);
        }
        pa = Enumerable.Repeat(-1, nm[0]).ToArray();
        Dijkstra(dic.Select(d => d.Values).ToArray(), 0);
        if (pa[nm[0] - 1] == -1) { WriteLine(-1);return; }
        var res = new List<int>() { nm[0] };
        for (int i = nm[0] - 1, j; (j = pa[i]) != -1; i = j)
            res.Add(j + 1);
        res.Reverse();

        WriteLine(string.Join(" ", res));
    }
    public static long[] Dijkstra(IList<IEnumerable<Pair<long, int>>> edges, int st)
    {
        var dist = Enumerable.Repeat(long.MaxValue, edges.Count).ToArray();
        var pq = new PriorityQueue<Pair<long, int>>(false);
        pq.Enqueue(new Pair<long, int>(0, st));
        dist[st] = 0;
        while (pq.Count != 0)
        {
            var p = pq.Dequeue();
            if (dist[p.v2] < p.v1) continue;
            foreach (var e in edges[p.v2])
                if (dist[e.v2] > e.v1 + dist[p.v2])
                {
                    dist[e.v2] = e.v1 + dist[p.v2];
                    pa[e.v2] = p.v2;
                    pq.Enqueue(new Pair<long, int>(dist[e.v2], e.v2));
                }
        }
        return dist;
    }
}

public class Input
{
    public static string read => ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public  static int num => ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public  static long numL => ToInt64(read);
    public static char[][] gred(int h)
        => Enumerable.Repeat(0, h).Select(_ => read.ToCharArray()).ToArray();
    public static int[] ar1D(int n)
        => Enumerable.Repeat(0, n).Select(_ => num).ToArray();
    public static long[] arL1D(int n)
        => Enumerable.Repeat(0, n).Select(_ => numL).ToArray();
    public static string[] strs(int n)
        => Enumerable.Repeat(0, n).Select(_ => read).ToArray();
    public static int[][] ar2D(int n)
        => Enumerable.Repeat(0, n).Select(_ => ar).ToArray();
    public static long[][] arL2D(int n)
        => Enumerable.Repeat(0, n).Select(_ => arL).ToArray();
    public static T getValue<T>(string g)
    {
        var t = typeof(T);
        if (t == typeof(int))
            return (T)(object)int.Parse(g);
        if (t == typeof(long))
            return (T)(object)long.Parse(g);
        if (t == typeof(string))
            return (T)(object)g;
        if (t == typeof(char))
            return (T)(object)char.Parse(g);
        if (t == typeof(double))
            return (T)(object)double.Parse(g);
        if (t == typeof(bool))
            return (T)(object)bool.Parse(g);
        return default(T);
    }
    public const long Inf = (long)1e18;
    public const double eps = 1e-6;
    public  const string Alfa = "abcdefghijklmnopqrstuvwxyz";
    public  const int MOD = 1000000007;
}
public class PriorityQueue<T> where T : IComparable<T>
{
    public List<T> _item;
    public int Count { get { return _item.Count; } }
    public bool IsMaxHeap { get; set; }
    public T Peek { get { return _item[0]; } }
    public PriorityQueue(bool IsMaxHeap = true) { _item = new List<T>(); this.IsMaxHeap = IsMaxHeap; }
    private int Compare(int i, int j) => (IsMaxHeap ? 1 : -1) * _item[i].CompareTo(_item[j]);
    private int Parent(int i)
        => (i - 1) >> 1;
    private int Left(int i)
        => (i << 1) + 1;
    public T Enqueue(T val)
    {
        int i = _item.Count;
        _item.Add(val);
        while (i > 0)
        {
            int p = Parent(i);
            if (Compare(i, p) > 0)
            {
                var tmp = _item[i];
                _item[i] = _item[p];
                _item[p] = tmp;
            }
            i = p;
        }
        return val;
    }
    public T Dequeue()
    {
        T val = _item[0];
        _item[0] = _item[_item.Count-1];
        _item.RemoveAt(_item.Count - 1);
        for (int i = 0, j; (j = Left(i)) < _item.Count; i = j)
        {
            if (j != _item.Count - 1 && Compare(j, j + 1) < 0) j++;
            if (Compare(i, j) < 0)
            {
                var tmp = _item[i];
                _item[i] = _item[j];
                _item[j] = tmp;
            }
        }
        return val;
    }
}

public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1 { get; set; }
    public T2 v2 { get; set; }
    public Pair() : this(default(T1), default(T2)) { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1;this.v2 = v2; }

    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public static Pair<T1, T2> MakePair()
    {
        var r = ReadLine().Split(' ');
        return new Pair<T1, T2>(Input.getValue<T1>(r[0]), Input.getValue<T2>(r[1]));
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
}


using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static int posDelUltimoElemento = -1;
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = Convert.ToInt32(ss[0]), m = Convert.ToInt32(ss[1]);
            ulong[][] matriz = new ulong[n][];
            Tuple<ulong, int>[] values = new Tuple<ulong, int>[n];
            int[] posEnElHeap = new int[n];
            ulong[] d = new ulong[n];
            int[] pi = new int[n];
            bool[] visit = new bool[n];
            for (int i = 0; i < n; i++)
            {
                matriz[i] = new ulong[n];
                posEnElHeap[i] = i;
                values[i] = new Tuple<ulong, int>(ulong.MaxValue, i);
                pi[i] = -1;
            }
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = Convert.ToInt32(ss[0]), b = Convert.ToInt32(ss[1]);
                ulong w = ulong.Parse(ss[2]);
                if (a != b)
                {
                    if (matriz[a - 1][b - 1] == 0)
                    {
                        matriz[a - 1][b - 1] = w;
                        matriz[b - 1][a - 1] = w;
                    }
                    else
                    {
                        matriz[a - 1][b - 1] = Math.Min(matriz[a - 1][b - 1], w);
                        matriz[b - 1][a - 1] = matriz[a - 1][b - 1];
                    }
                }
            }
            posDelUltimoElemento = n - 1;
            visit[0] = true;
            values[0] = new Tuple<ulong, int>(0, 0);
            DijkstraAlgorithms(matriz, values, d, posEnElHeap, pi, visit);
            if (!visit[n - 1]) Console.WriteLine(-1);
            else
            {
                Stack<int> st = new Stack<int>();
                st.Push(n - 1);
                int father = n - 1;
                while (pi[father] != 0)
                {
                    father = pi[father];
                    st.Push(father);
                }
                Console.Write(1);
                while (st.Count > 0) Console.Write(" " + (st.Pop() + 1));
            }
        }
        private static void DijkstraAlgorithms(ulong[][] matriz, Tuple<ulong, int>[] values, ulong[] d, int[] posEnElHeap, int[] pi, bool[] visit)
        {
            Tuple<ulong, int> temp;
            while (posDelUltimoElemento > -1)
            {
                temp = MinHeap.Minimum(values, posDelUltimoElemento);
                MinHeap.Min_Heap_Delete(values, posEnElHeap, temp.Item2, ref posDelUltimoElemento);
                for (int i = 0; i < matriz.Length; i++)
                {
                    if (matriz[temp.Item2][i] != 0)
                    {
                        if (!visit[i])//no me intereza el valor de distanca es infinito;
                        {
                            d[i] = d[temp.Item2] + matriz[temp.Item2][i];
                            pi[i] = temp.Item2;
                            MinHeap.Heap_Decrease_Key(values, posEnElHeap, posEnElHeap[i], new Tuple<ulong, int>(d[i], i));
                            visit[i] = true;
                        }
                        else if (d[i] > d[temp.Item2] + matriz[temp.Item2][i])
                        {
                            d[i] = d[temp.Item2] + matriz[temp.Item2][i];
                            pi[i] = temp.Item2;
                            MinHeap.Heap_Decrease_Key(values, posEnElHeap, posEnElHeap[i], new Tuple<ulong, int>(d[i], i));
                        }
                    }
                }
            }
        }
    }
    class MinHeap
    {
        public static int Parent(int i)
        {
            if (i % 2 == 0) return (i / 2 - 1);
            return i / 2;
        }
        public static int Left(int father)
        {
            return 2 * father + 1;
        }
        public static int Right(int father)
        {
            return 2 * father + 2;
        }
        public static void Min_Heapify(Tuple<ulong, int>[] array, int[] posEnElHeap, int pos, int posDelUltimoElemento)
        {
            int posHijoIzquierdo = Left(pos);
            int posHijoDerecho = Right(pos);
            int cambio = 0;
            if (posHijoIzquierdo <= posDelUltimoElemento && array[posHijoIzquierdo].Item1 < array[pos].Item1) cambio = posHijoIzquierdo;
            else cambio = pos;
            if (posHijoDerecho <= posDelUltimoElemento && array[posHijoDerecho].Item1 < array[cambio].Item1) cambio = posHijoDerecho;
            if (cambio != pos)
            {
                Tuple<ulong, int> temp = array[cambio];
                posEnElHeap[array[pos].Item2] = cambio;
                posEnElHeap[array[cambio].Item2] = pos;
                array[cambio] = array[pos];
                array[pos] = temp;
                Min_Heapify(array, posEnElHeap, cambio, posDelUltimoElemento);
            }
        }
        public static Tuple<ulong, int> Minimum(Tuple<ulong, int>[] a, int posDelUltimoElemento)
        {
            if (posDelUltimoElemento < 0) throw new InvalidOperationException();
            return a[0];
        }
        public static void Heap_Decrease_Key(Tuple<ulong, int>[] array, int[] posEnELHeap, int i, Tuple<ulong, int> key)
        {
            if (key.Item1 > array[i].Item1) throw new InvalidOperationException("la llave es mayor");
            array[i] = key;
            int p = Parent(i);
            while (i > 0 && array[p].Item1 > array[i].Item1)
            {
                Tuple<ulong, int> temp = array[i];
                posEnELHeap[array[i].Item2] = p;
                posEnELHeap[array[p].Item2] = i;
                array[i] = array[p];
                array[p] = temp;
                i = p;
                p = Parent(i);
            }
        }
        public static void Min_Heap_Delete(Tuple<ulong, int>[] array, int[] posEnElHeap, int key, ref int posDelUltimoElemento)
        {
            int pos = posEnElHeap[key];
            Tuple<ulong, int> ultimoElemen = array[posDelUltimoElemento];
            array[pos] = ultimoElemen;
            posDelUltimoElemento--;
            posEnElHeap[ultimoElemen.Item2] = pos;
            Min_Heapify(array, posEnElHeap, pos, posDelUltimoElemento);
        }
    }
}


using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]), m = int.Parse(ss[1]);
            List<Tuple<int, int>>[] adj = new List<Tuple<int, int>>[n];
            int[] pi = new int[n];
            int[] d = new int[n];
            bool[] visit = new bool[n];
            for (int i = 0; i < n; i++)
            {
                adj[i] = new List<Tuple<int, int>>();
                d[i] = int.MaxValue;
                pi[i] = -1;
            }
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = int.Parse(ss[0]), b = int.Parse(ss[1]), w = int.Parse(ss[2]);
                adj[a - 1].Add(new Tuple<int, int>(w, b - 1));
                adj[b - 1].Add(new Tuple<int, int>(w, a - 1));
            }
            d[0] = 0;
            visit[0] = true;
            SortedSet<Tuple<int, int>> h = new SortedSet<Tuple<int, int>>();
            Tuple<int, int> temp;
            h.Add(new Tuple<int, int>(0, 0));
            for (int i = 1; i < n; i++) h.Add(new Tuple<int, int>(int.MaxValue, i));
            while (h.Count > 0)
            {
                temp = h.Min;
                h.Remove(h.Min);
                for (int i = 0; i < adj[temp.Item2].Count; i++)
                {
                    if (!visit[adj[temp.Item2][i].Item2])
                    {
                        h.Remove(new Tuple<int, int>(int.MaxValue, adj[temp.Item2][i].Item2));
                        visit[adj[temp.Item2][i].Item2] = true;
                        pi[adj[temp.Item2][i].Item2] = temp.Item2;
                        d[adj[temp.Item2][i].Item2] = d[temp.Item2] + adj[temp.Item2][i].Item1;
                        h.Add(new Tuple<int, int>(int.MaxValue, adj[temp.Item2][i].Item2));
                    }
                    else if (d[adj[temp.Item2][i].Item2] > d[temp.Item2] + adj[temp.Item2][i].Item1)
                    {
                        h.Remove(new Tuple<int, int>(int.MaxValue, adj[temp.Item2][i].Item2));
                        d[adj[temp.Item2][i].Item2] = d[temp.Item2] + adj[temp.Item2][i].Item1;
                        pi[adj[temp.Item2][i].Item2] = temp.Item2;
                        h.Add(new Tuple<int, int>(int.MaxValue, adj[temp.Item2][i].Item2));
                    }
                }
            }
            if (!visit[n - 1]) Console.WriteLine(-1);
            else
            {
                Stack<int> st = new Stack<int>();
                st.Push(n - 1);
                int father = n - 1;
                while (pi[father] != 0)
                {
                    father = pi[father];
                    st.Push(father);
                }
                Console.Write(1);
                while (st.Count > 0) Console.Write(" " + (st.Pop() + 1));
            }
        }
    }
}
//EZIEL


using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static int posDelUltimoElemento = -1;
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = Convert.ToInt32(ss[0]), m = Convert.ToInt32(ss[1]);
            int[][] matriz = new int[n][];
            Tuple<int, int>[] values = new Tuple<int, int>[n];
            int[] posEnElHeap = new int[n];
            int[] d = new int[n];
            int[] pi = new int[n];
            bool[] visit = new bool[n];
            for (int i = 0; i < n; i++)
            {
                matriz[i] = new int[n];
                posEnElHeap[i] = i;
                values[i] = new Tuple<int, int>(int.MaxValue, i);
                pi[i] = -1;
            }
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = Convert.ToInt32(ss[0]), b = Convert.ToInt32(ss[1]), w = Convert.ToInt32(ss[2]);
                if (a != b)
                {
                    if (matriz[a - 1][b - 1] == 0) matriz[a - 1][b - 1] = w;
                    else matriz[a - 1][b - 1] = Math.Min(matriz[a - 1][b - 1], w);
                }
            }
            posDelUltimoElemento = n - 1;
            visit[0] = true;
            values[0] = new Tuple<int, int>(0, 0);
            DijkstraAlgorithms(matriz, values, d, posEnElHeap, pi, visit);
            if (!visit[n - 1]) Console.WriteLine(-1);
            else
            {
                Stack<int> st = new Stack<int>();
                st.Push(n - 1);
                int father = n - 1;
                while (pi[father] != 0)
                {
                    father = pi[father];
                    st.Push(father);
                }
                Console.Write(1);
                while (st.Count > 0) Console.Write(" " + (st.Pop() + 1));
            }
        }
        private static void DijkstraAlgorithms(int[][] matriz, Tuple<int, int>[] values, int[] d, int[] posEnElHeap, int[] pi, bool[] visit)
        {
            Tuple<int, int> temp;
            while (posDelUltimoElemento > -1)
            {
                temp = MinHeap.Minimum(values, posDelUltimoElemento);
                MinHeap.Min_Heap_Delete(values, posEnElHeap, temp.Item2, ref posDelUltimoElemento);
                for (int i = 0; i < matriz.Length; i++)
                {
                    if (matriz[temp.Item2][i] != 0)
                    {
                        if (!visit[i])//no me intereza el valor de distanca es infinito;
                        {
                            d[i] = d[temp.Item2] + matriz[temp.Item2][i];
                            pi[i] = temp.Item2;
                            MinHeap.Heap_Decrease_Key(values, posEnElHeap, posEnElHeap[i], new Tuple<int, int>(d[i], i));
                            visit[i] = true;
                        }
                        else if (d[i] > d[temp.Item2] + matriz[temp.Item2][i])
                        {
                            d[i] = d[temp.Item2] + matriz[temp.Item2][i];
                            pi[i] = temp.Item2;
                            MinHeap.Heap_Decrease_Key(values, posEnElHeap, posEnElHeap[i], new Tuple<int, int>(d[i], i));
                        }
                    }
                }
            }
        }
    }
    class MinHeap
    {
        public static int Parent(int i)
        {
            if (i % 2 == 0) return (i / 2 - 1);
            return i / 2;
        }
        public static int Left(int father)
        {
            return 2 * father + 1;
        }
        public static int Right(int father)
        {
            return 2 * father + 2;
        }
        public static void Min_Heapify(Tuple<int, int>[] array, int[] posEnElHeap, int pos, int posDelUltimoElemento)
        {
            int posHijoIzquierdo = Left(pos);
            int posHijoDerecho = Right(pos);
            int cambio = 0;
            if (posHijoIzquierdo <= posDelUltimoElemento && array[posHijoIzquierdo].Item1 < array[pos].Item1) cambio = posHijoIzquierdo;
            else cambio = pos;
            if (posHijoDerecho <= posDelUltimoElemento && array[posHijoDerecho].Item1 < array[cambio].Item1) cambio = posHijoDerecho;
            if (cambio != pos)
            {
                Tuple<int, int> temp = array[cambio];
                posEnElHeap[array[pos].Item2] = cambio;
                posEnElHeap[array[cambio].Item2] = pos;
                array[cambio] = array[pos];
                array[pos] = temp;
                Min_Heapify(array, posEnElHeap, cambio, posDelUltimoElemento);
            }
        }
        public static Tuple<int, int> Minimum(Tuple<int, int>[] a, int posDelUltimoElemento)
        {
            if (posDelUltimoElemento < 0) throw new InvalidOperationException();
            return a[0];
        }
        public static void Heap_Decrease_Key(Tuple<int, int>[] array, int[] posEnELHeap, int i, Tuple<int, int> key)
        {
            if (key.Item1 > array[i].Item1) throw new InvalidOperationException("la llave es mayor");
            array[i] = key;
            posEnELHeap[i] = i;
            int p = Parent(i);
            while (i > 0 && array[p].Item1 > array[i].Item1)
            {
                Tuple<int, int> temp = array[i];
                posEnELHeap[array[i].Item2] = p;
                posEnELHeap[array[p].Item2] = i;
                array[i] = array[p];
                array[p] = temp;
                i = p;
                p = Parent(i);
            }
        }
        public static void Min_Heap_Delete(Tuple<int, int>[] array, int[] posEnElHeap, int key, ref int posDelUltimoElemento)
        {
            int pos = posEnElHeap[key];
            Tuple<int, int> ultimoElemen = array[posDelUltimoElemento];
            array[pos] = ultimoElemen;
            posDelUltimoElemento--;
            posEnElHeap[ultimoElemen.Item2] = pos;
            Min_Heapify(array, posEnElHeap, pos, posDelUltimoElemento);
        }
    }
}


using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]), m = int.Parse(ss[1]);
            int[][] adj = new int[n][];
            for (int i = 0; i < n; i++) adj[i] = new int[n];
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = int.Parse(ss[0]) - 1, b = int.Parse(ss[1]) - 1, w = int.Parse(ss[2]);
                if (a != b) adj[a][b] = adj[b][a] = (adj[a][b] == 0 ? w : Math.Min(w, adj[a][b]));
            }
            int[] d = new int[n];
            int[] pi = new int[n];
            bool[] visit = new bool[n];
            SortedSet<Tuple<int, int>> h = new SortedSet<Tuple<int, int>>();
            h.Add(new Tuple<int, int>(0, 0));
            for (int i = 1; i < n; i++)
            {
                pi[i] = -1;
                d[i] = int.MaxValue / 2;
                h.Add(new Tuple<int, int>(int.MaxValue / 2, i));
            }
            d[0] = 0;
            visit[0] = true;
            Tuple<int, int> temp;
            while (h.Count > 0 && (h.Min.Item1 != (int.MaxValue / 2)))
            {
                temp = h.Min;
                h.Remove(h.Min);
                for (int i = 0; i < n; i++)
                {
                    if (adj[temp.Item2][i] != 0)
                    {
                        if (!visit[i])
                        {
                            h.Remove(new Tuple<int, int>(d[i], i));
                            visit[i] = true;
                            d[i] = d[temp.Item2] + adj[temp.Item2][i];
                            h.Add((new Tuple<int, int>(d[i], i)));
                            pi[i] = temp.Item2;
                        }
                        else if (d[i] > d[temp.Item2] + adj[temp.Item2][i])
                        {
                            h.Remove(new Tuple<int, int>(d[i], i));
                            d[i] = d[temp.Item2] + adj[temp.Item2][i];
                            pi[i] = temp.Item2;
                            h.Add(new Tuple<int, int>(d[i], i));
                        }
                    }
                }
            }
            if (!visit[n - 1]) Console.WriteLine(-1);
            else
            {
                Stack<int> st = new Stack<int>();
                st.Push(n - 1);
                int father = n - 1;
                while (pi[father] != 0)
                {
                    father = pi[father];
                    st.Push(father);
                }
                Console.Write(1);
                while (st.Count > 0) Console.Write(" " + (st.Pop() + 1));
            }
            Console.WriteLine();
        }
    }
}
//EZIEL


using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]), m = int.Parse(ss[1]), A = 0, B = n - 1;
            List<Tuple>[] adj = new List<Tuple>[n];
            for (int i = 0; i < n; i++) adj[i] = new List<Tuple>();
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = int.Parse(ss[0]) - 1, b = int.Parse(ss[1]) - 1;
                long w = int.Parse(ss[2]);
                adj[a].Add(new Tuple(w, b));
                adj[b].Add(new Tuple(w, a));
            }
            long[] d = new long[n];
            int[] pi = new int[n];
            bool[] visit = new bool[n];
            Heap h = new Heap(n);
            for (int i = 0; i < n; i++)
            {
                pi[i] = -1;
                d[i] = int.MaxValue / 2;
                if (i < A)
                {
                    h.array[i + 1] = new Tuple(long.MaxValue / 2, i);
                    h.posEnELHeap[i] = i + 1;
                }
                else if (i > A)
                {
                    h.array[i] = new Tuple(long.MaxValue / 2, i);
                    h.posEnELHeap[i] = i;
                }
            }
            h.array[0] = new Tuple(0, A);
            h.posEnELHeap[A] = 0;
            h.posDelUltimoElemento = n - 1;
            d[A] = 0;
            visit[A] = true;
            Tuple temp;
            while (h.posDelUltimoElemento + 1 > 0 && (h.Minimum().item1 != (long.MaxValue / 2)))
            {
                temp = h.Minimum();
                h.Min_Heap_Delete(h.Minimum());
                foreach (var item in adj[temp.item2])
                {
                    if (!visit[item.item2])
                    {
                        visit[item.item2] = true;
                        d[item.item2] = d[temp.item2] + item.item1;
                        h.Heap_Decrease_Key(new Tuple(d[item.item2], item.item2));
                        pi[item.item2] = temp.item2;
                    }
                    else if (d[item.item2] > d[temp.item2] + item.item1)
                    {
                        d[item.item2] = d[temp.item2] + item.item1;
                        pi[item.item2] = temp.item2;
                        h.Heap_Decrease_Key(new Tuple(d[item.item2], item.item2));
                    }
                }
            }
            int parent = B;
            Stack<int> p = new Stack<int>();
            while (parent != -1)
            {
                p.Push(parent + 1);
                parent = pi[parent];
            }
            if (p.Count > 1)
            {
                Console.Write(p.Pop());
                foreach (var item in p) Console.Write(" " + item);
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine(-1);
            }
        }
    }
    class Heap
    {
        public Tuple[] array;
        public int posDelUltimoElemento = -1;
        public int[] posEnELHeap;
        public Heap(int n)
        {
            posDelUltimoElemento = -1;
            array = new Tuple[n];
            posEnELHeap = new int[n];
        }
        int Parent(int i)
        {
            if (i % 2 == 0) return (i / 2 - 1);
            return i / 2;
        }
        int Left(int father)
        {
            return 2 * father + 1;
        }
        int Right(int father)
        {
            return 2 * father + 2;
        }
        public void Min_Heapify(int pos)
        {
            int posHijoIzquierdo = Left(pos);
            int posHijoDerecho = Right(pos);
            int cambio = 0;
            if (posHijoIzquierdo <= posDelUltimoElemento && (array[posHijoIzquierdo].CompareTo(array[pos]) < 0)) cambio = posHijoIzquierdo;
            else cambio = pos;
            if (posHijoDerecho <= posDelUltimoElemento && (array[posHijoDerecho].CompareTo(array[cambio]) < 0)) cambio = posHijoDerecho;
            if (cambio != pos)
            {
                Tuple temp = array[cambio];
                Tuple temp2 = array[pos];
                array[cambio] = array[pos];
                array[pos] = temp;
                int index = posEnELHeap[temp.item2];
                posEnELHeap[temp.item2] = posEnELHeap[temp2.item2];
                posEnELHeap[temp2.item2] = index;
                Min_Heapify(cambio);
            }
        }
        public Tuple Minimum()
        {
            if (posDelUltimoElemento < 0) throw new InvalidOperationException();
            return array[0];
        }
        public void Heap_Decrease_Key(Tuple key)
        {
            int i = posEnELHeap[key.item2];
            if (key.CompareTo(array[i]) > 0) throw new InvalidOperationException("la llave es mayor");
            array[i] = key;
            int p = Parent(i);
            while (i > 0 && array[p].CompareTo(array[i]) > 0)
            {
                Tuple temp = array[i];
                Tuple temp2 = array[p];
                array[i] = array[p];
                array[p] = temp;
                int index = posEnELHeap[temp.item2];
                posEnELHeap[temp.item2] = posEnELHeap[temp2.item2];
                posEnELHeap[temp2.item2] = index;
                i = p;
                p = Parent(i);
            }
        }
        public void Min_Heap_Insert(Tuple key)
        {
            posDelUltimoElemento++;
            array[posDelUltimoElemento] = key;
            posEnELHeap[key.item2] = posDelUltimoElemento;
            Heap_Decrease_Key(key);
        }
        public void Min_Heap_Delete(Tuple key)
        {
            int pos = posEnELHeap[key.item2];
            Tuple ultimoElemen = array[posDelUltimoElemento];
            array[pos] = ultimoElemen;
            posEnELHeap[ultimoElemen.item2] = pos;
            posDelUltimoElemento--;
            Min_Heapify(pos);
        }
        public void BuildHeap(Tuple[] array)
        {
            throw new NotImplementedException();
        }
    }
    class Tuple : IComparable<Tuple>
    {
        public long item1;
        public int item2;
        public Tuple(long x, int y)
        {
            item1 = x;
            item2 = y;
        }
        public int CompareTo(Tuple other)
        {
            if (item1 == other.item1)
            {
                if (item2 < other.item2) return -1;
                if (item2 == other.item2) return 0;
                return 1;
            }
            if (item1 < other.item1) return -1;
            return 1;
        }
    }
}
//EZIEL


using System;
using System.Collections.Generic;
namespace Problem___20C___Codeforces
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] ss = Console.ReadLine().Split();
            int n = int.Parse(ss[0]), m = int.Parse(ss[1]);
            List<Tuple<int, int>>[] adj = new List<Tuple<int, int>>[n];
            int[] pi = new int[n];
            int[] d = new int[n];
            bool[] visit = new bool[n];
            for (int i = 0; i < n; i++)
            {
                adj[i] = new List<Tuple<int, int>>();
                d[i] = int.MaxValue;
                pi[i] = -1;
            }
            for (int i = 0; i < m; i++)
            {
                ss = Console.ReadLine().Split();
                int a = int.Parse(ss[0]), b = int.Parse(ss[1]), w = int.Parse(ss[2]);
                adj[a - 1].Add(new Tuple<int, int>(w, b - 1));
                adj[b - 1].Add(new Tuple<int, int>(w, a - 1));
            }
            d[0] = 0;
            visit[0] = true;
            SortedSet<Tuple<int, int>> h = new SortedSet<Tuple<int, int>>();
            Tuple<int, int> temp;
            h.Add(new Tuple<int, int>(0, 0));
            while (h.Count > 0)
            {
                temp = h.Min;
                h.Remove(h.Min);
                for (int i = 0; i < adj[temp.Item2].Count; i++)
                {
                    if (!visit[adj[temp.Item2][i].Item2])
                    {
                        visit[adj[temp.Item2][i].Item2] = true;
                        pi[adj[temp.Item2][i].Item2] = temp.Item2;
                        d[adj[temp.Item2][i].Item2] = d[temp.Item2] + adj[temp.Item2][i].Item1;
                        h.Add(adj[temp.Item2][i]);
                    }
                    else if (d[adj[temp.Item2][i].Item2] > d[temp.Item2] + adj[temp.Item2][i].Item1)
                    {
                        d[adj[temp.Item2][i].Item2] = d[temp.Item2] + adj[temp.Item2][i].Item1;
                        pi[adj[temp.Item2][i].Item2] = temp.Item2;
                    }
                }
            }
            if (!visit[n - 1]) Console.WriteLine(-1);
            else
            {
                Stack<int> st = new Stack<int>();
                st.Push(n - 1);
                int father = n - 1;
                while (pi[father] != 0)
                {
                    father = pi[father];
                    st.Push(father);
                }
                Console.Write(1);
                while (st.Count > 0) Console.Write(" " + (st.Pop() + 1));
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    bool b = false;

                    foreach (var item in lst[mas[0]])
                    {
                        if (item.k == mas[1] && item.w > mas[2])
                        {
                            item.w = mas[2];
                            foreach (var it in lst[mas[1]])
                            {
                                if (it.k == mas[0] && item.w > mas[2])
                                {
                                    item.w = mas[2];
                                    break;
                                }
                            }
                            b = true;
                            break;
                        }

                    }
                    if (!b)
                    {
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
              dist[0]=0;
            color[0]=1;
            while (true)
            {
                int id = -1;
                for (int i = 0; i < n; i++)
                {
                    if(color[i]==1)
                        if (id == -1 || dist[id] > dist[i])
                        {
                            id = i;
                        }

                }
                if (id == -1) break;
                color[id] = 2;
                foreach (var i in lst[id])
                {
                    long nov = dist[id] + i.w;
                    if (color[i.k] == 0 || nov < dist[i.k])
                    {
                        color[i.k] = 1;
                        dist[i.k] = nov;
                        fr[i.k] = id;

                    }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
    }
    class Graph
    {
        public int k;
        public int w;
        public Graph(int kk, int ww)
        {
            k = kk;
            w = ww;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static List<Graph> gr = new List<Graph>();

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {
                        dic[str] = mas[2];
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            gr.Add(new Graph(0,0));

            while (gr.Count>0)
            {
                Graph v = gr[0];
                gr.RemoveAt(0);
                color[v.k] = 2;
                foreach (var i in lst[v.k])
                {
                    long nov = v.w + i.w;
                    if (color[i.k] == 0)
                    {
                        gr.Add(new Graph(i.k,nov));
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = v.k;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = v.k;

                        }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
        static public void update(int id,long w)
        {
            for (int i = 0; i < gr.Count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }
    class Graph
    {
        public int k;
        public long w;
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static List<Graph> gr = new List<Graph>();

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {
                        dic[str] = mas[2];
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
              dist[0]=0;
            color[0]=1;

            gr.Add(new Graph(0,0));

            while (gr.Count>0)
            {
                Graph v = gr[0];
                gr.RemoveAt(0);
                color[v.k] = 2;
                foreach (var i in lst[v.k])
                {
                    long nov = dist[v.k] + i.w;
                    if (color[i.k] == 0)
                    {
                        gr.Add(new Graph(i.k,nov));
                        color[i.k] = 1;
                        dist[i.k] = nov;
                        fr[i.k] = v.k;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = v.k;

                        }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
        static public void update(int id,long w)
        {
            for (int i = 0; i < gr.Count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }
    class Graph
    {
        public int k;
        public long w;
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {

        static public Graph[] gr;
        static public int count;

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            gr = new Graph[n*2+10];
            for (int i = 0; i < n; i++)
            {
                lst[i] = new List<Graph>();

            }
            for (int i = 0; i < n*2+10; i++)
            {
                gr[i] = new Graph();

            }
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            #region begin
            for (int i = 0; i < m; i++)
            {

                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];

                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            #endregion

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            count = 1;
            Add(0,0);
            while (count>1)
            {
                //Graph v = gr[0];
                int vv = gr[1].k;
                Remove();
                color[vv] = 1;
                foreach (var i in lst[vv])
                {
                    long nov = dist[vv] + i.w;
                    if (color[i.k] == 0)
                    {
                        Add(i.k,nov);
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = vv;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = vv;
                            color[i.k] = 2;

                        }

                }

            }
            #region end
            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);

            }
            #endregion

        }
        static public void Add(int  id ,long w)
        {

            gr[count].k = id;
            gr[count].w = w;

            int tek = count;
            int parent = tek/2;
            while (tek>1)
            {
                if (gr[tek].w < gr[parent].w)
                {
                    int kk = gr[parent].k;
                    long ww = gr[parent].w;
                    gr[parent].k = gr[tek].k;
                    gr[parent].w = gr[tek].w;
                    gr[tek].k = kk;
                    gr[tek].w = ww;
                    tek = parent;
                    parent = tek / 2;
                }
                else
                    break;
            }
            count++;

        }
        static public void Remove()
        {
            count--;
            gr[1].k = gr[count].k;
            gr[1].w = gr[count].w;
            int kk = gr[1].k;
            long ww = gr[1].w;

            int tek = 1;
            while (tek<count-1)
            {
                if (tek*2>=count || (gr[tek].w < gr[tek * 2].w && gr[tek].w < gr[tek * 2 + 1].w))
                    break;
                if (gr[tek * 2].w < gr[tek * 2 + 1].w  || tek*2+1>=count)
                {
                    gr[tek].w = gr[tek * 2].w;
                    gr[tek].k = gr[tek * 2].k;
                    gr[tek * 2].w = ww;
                    gr[tek * 2].k = kk;
                    tek = tek * 2;
                }
                else
                {
                    gr[tek].w = gr[tek * 2+1].w;
                    gr[tek].k = gr[tek * 2+1].k;
                    gr[tek * 2+1].w = ww;
                    gr[tek * 2+1].k = kk;
                    tek = tek * 2+1;
                }

            }
        }

        static public void update(int id,long w)
        {
            for (int i = 1; i < count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }

    class Graph
    {
        public int k;
        public long w;
        public Graph() { }
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            bool[,] mm = new bool[n, n];
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if(mm[mas[0],mas[1]])
                    {
                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }
                                break;
                            }

                        }
                    }
                    else
                    {
                        mm[mas[0], mas[1]] = true;
                        mm[mas[1], mas[0]] = true;
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
              dist[0]=0;
            color[0]=1;
            while (true)
            {
                int id = -1;
                for (int i = 0; i < n; i++)
                {
                    if(color[i]==1)
                        if (id == -1 || dist[id] > dist[i])
                        {
                            id = i;
                        }

                }
                if (id == -1) break;
                color[id] = 2;
                foreach (var i in lst[id])
                {
                    long nov = dist[id] + i.w;
                    if (color[i.k] == 0 || nov < dist[i.k])
                    {
                        color[i.k] = 1;
                        dist[i.k] = nov;
                        fr[i.k] = id;

                    }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
    }
    class Graph
    {
        public int k;
        public int w;
        public Graph(int kk, int ww)
        {
            k = kk;
            w = ww;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static List<Graph> gr = new List<Graph>();

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
         //   Dictionary<string, int> dic = new Dictionary<string, int>();
          //  string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
            //    str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
              /*      if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];
               */
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                 //   }
                }

            }

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            gr.Add(new Graph(0,0));

            while (gr.Count>0)
            {
                int t = Take();
                Graph v = gr[t];
                gr.RemoveAt(t);
                color[v.k] = 1;
                foreach (var i in lst[v.k])
                {
                    long nov = v.w + i.w;
                    if (color[i.k] == 0)
                    {
                        gr.Add(new Graph(i.k,nov));
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = v.k;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = v.k;
                            color[i.k] = 2;

                        }

                }
            //    Sort();

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
        static public int Take()
        {
            long min=gr[0].w;
            int t =0;
            for (int i = 1; i < gr.Count; i++)
                if (min > gr[i].w)
                {
                    min = gr[i].w;
                    t = i;

                }
            return t;

        }
        static public void update(int id,long w)
        {
            for (int i = 0; i < gr.Count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }
    class Graph
    {
        public int k;
        public long w;
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static List<Graph> gr = new List<Graph>();

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
         //   Dictionary<string, int> dic = new Dictionary<string, int>();
          //  string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
            //    str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
              /*      if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];
               */
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                 //   }
                }

            }

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            gr.Add(new Graph(0,0));

            while (gr.Count>0)
            {
                Graph v = gr[0];
                gr.RemoveAt(0);
                color[v.k] = 1;
                foreach (var i in lst[v.k])
                {
                    long nov = v.w + i.w;
                    if (color[i.k] == 0)
                    {
                        gr.Add(new Graph(i.k,nov));
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = v.k;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = v.k;
                            color[i.k] = 2;

                        }

                }
                Sort();

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
        static public void Sort()
        {
            for (int i = 0; i < gr.Count; i++)
                for (int j = i+1; j < gr.Count; j++)
                {
                    if (gr[j].w < gr[i].w)
                    {
                        long gg1= gr[j].w;
                        int gg2 = gr[j].k;
                        gr[j].k = gr[i].k;
                        gr[j].w = gr[i].w;
                        gr[i].k = gg2;
                        gr[i].w = gg1;

                    }

                }
            {

            }
        }
        static public void update(int id,long w)
        {
            for (int i = 0; i < gr.Count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }
    class Graph
    {
        public int k;
        public long w;
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static List<Graph> gr = new List<Graph>();

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];

                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            gr.Add(new Graph(0,0));

            while (gr.Count>0)
            {
                int t = Take();
                Graph v = gr[t];
                gr.RemoveAt(t);
                color[v.k] = 1;
                foreach (var i in lst[v.k])
                {
                    long nov = v.w + i.w;
                    if (color[i.k] == 0)
                    {
                        gr.Add(new Graph(i.k,nov));
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = v.k;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = v.k;
                            color[i.k] = 2;

                        }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
        static public int Take()
        {
            long min=gr[0].w;
            int t =0;
            for (int i = 1; i < gr.Count; i++)
                if (min > gr[i].w)
                {
                    min = gr[i].w;
                    t = i;

                }
            return t;

        }
        static public void update(int id,long w)
        {
            for (int i = 0; i < gr.Count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }
    class Graph
    {
        public int k;
        public long w;
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {

        static public Graph[] gr;
        static public int count;

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n+10];
            gr = new Graph[n+10];
            for (int i = 0; i < n+5; i++)
            {
                lst[i] = new List<Graph>();
                gr[i] = new Graph();
            }
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            #region begin
            for (int i = 0; i < m; i++)
            {

                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];

                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            #endregion

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            count = 1;
            Add(0,0);
            while (count>1)
            {
                //Graph v = gr[0];
                int vv = gr[1].k;
                Remove();
                color[vv] = 1;
                foreach (var i in lst[vv])
                {
                    long nov = dist[vv] + i.w;
                    if (color[i.k] == 0)
                    {
                        Add(i.k,nov);
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = vv;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = vv;
                            color[i.k] = 2;

                        }

                }

            }
            #region end
            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);

            }
            #endregion

        }
        static public void Add(int  id ,long w)
        {

            gr[count].k = id;
            gr[count].w = w;

            int tek = count;
            int parent = tek/2;
            while (tek!=1)
            {
                if (gr[tek].w < gr[parent].w)
                {
                    int kk = gr[parent].k;
                    long ww = gr[parent].w;
                    gr[parent].k = gr[tek].k;
                    gr[parent].w = gr[tek].w;
                    gr[tek].k = kk;
                    gr[tek].w = ww;
                    tek = parent;
                    parent = tek / 2;
                }
                else
                    break;
            }
            count++;

        }
        static public void Remove()
        {
            count--;
            gr[1].k = gr[count].k;
            gr[1].w = gr[count].w;
            int kk = gr[1].k;
            long ww = gr[1].w;

            int tek = 1;
            while (tek<count-1)
            {
                if (gr[tek].w < gr[tek * 2].w && gr[tek].w < gr[tek * 2 + 1].w)
                    break;
                if (gr[tek * 2].w < gr[tek * 2 + 1].w)
                {
                    gr[tek].w = gr[tek * 2].w;
                    gr[tek].k = gr[tek * 2].k;
                    gr[tek * 2].w = ww;
                    gr[tek * 2].k = kk;
                    tek = tek * 2;
                }
                else
                {
                    gr[tek].w = gr[tek * 2+1].w;
                    gr[tek].k = gr[tek * 2+1].k;
                    gr[tek * 2+1].w = ww;
                    gr[tek * 2+1].k = kk;
                    tek = tek * 2+1;
                }

            }
        }

        static public void update(int id,long w)
        {
            for (int i = 1; i < count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }

    class Graph
    {
        public int k;
        public long w;
        public Graph() { }
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {

        static public Graph[] gr;
        static public int count;

        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n+10];
            gr = new Graph[n+10];
            for (int i = 0; i < n+8; i++)
            {
                lst[i] = new List<Graph>();
                gr[i] = new Graph();
            }
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            #region begin
            for (int i = 0; i < m; i++)
            {

                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {

                        dic[str] = mas[2];

                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            #endregion

            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
            dist[0]=0;
            count = 1;
            Add(0,0);
            while (count>1)
            {
                //Graph v = gr[0];
                int vv = gr[1].k;
                Remove();
                color[vv] = 1;
                foreach (var i in lst[vv])
                {
                    long nov = dist[vv] + i.w;
                    if (color[i.k] == 0)
                    {
                        Add(i.k,nov);
                        color[i.k] = 2;
                        dist[i.k] = nov;
                        fr[i.k] = vv;
                    }
                    else
                        if (nov < dist[i.k])
                        {
                            update(i.k,nov);
                            dist[i.k] = nov;
                            fr[i.k] = vv;
                            color[i.k] = 2;

                        }

                }

            }
            #region end
            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);

            }
            #endregion

        }
        static public void Add(int  id ,long w)
        {

            gr[count].k = id;
            gr[count].w = w;

            int tek = count;
            int parent = tek/2;
            while (tek>1)
            {
                if (gr[tek].w < gr[parent].w)
                {
                    int kk = gr[parent].k;
                    long ww = gr[parent].w;
                    gr[parent].k = gr[tek].k;
                    gr[parent].w = gr[tek].w;
                    gr[tek].k = kk;
                    gr[tek].w = ww;
                    tek = parent;
                    parent = tek / 2;
                }
                else
                    break;
            }
            count++;

        }
        static public void Remove()
        {
            count--;
            gr[1].k = gr[count].k;
            gr[1].w = gr[count].w;
            int kk = gr[1].k;
            long ww = gr[1].w;

            int tek = 1;
            while (tek<count-1)
            {
                if (gr[tek].w < gr[tek * 2].w && gr[tek].w < gr[tek * 2 + 1].w)
                    break;
                if (gr[tek * 2].w < gr[tek * 2 + 1].w)
                {
                    gr[tek].w = gr[tek * 2].w;
                    gr[tek].k = gr[tek * 2].k;
                    gr[tek * 2].w = ww;
                    gr[tek * 2].k = kk;
                    tek = tek * 2;
                }
                else
                {
                    gr[tek].w = gr[tek * 2+1].w;
                    gr[tek].k = gr[tek * 2+1].k;
                    gr[tek * 2+1].w = ww;
                    gr[tek * 2+1].k = kk;
                    tek = tek * 2+1;
                }

            }
        }

        static public void update(int id,long w)
        {
            for (int i = 1; i < count; i++)
            {
                if (gr[i].k == id)
                {
                    gr[i].w = w;
                    break;

                }

            }

        }
    }

    class Graph
    {
        public int k;
        public long w;
        public Graph() { }
        public Graph(int kk,long ww)
        {
            k = kk;
            w = ww;
        }

       }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    bool b = false;

                    /*
                    foreach (var item in lst[mas[0]])
                    {
                        if (item.k == mas[1] && item.w > mas[2])
                        {
                            item.w = mas[2];
                            foreach (var it in lst[mas[1]])
                            {
                                if (it.k == mas[0] && item.w > mas[2])
                                {
                                    item.w = mas[2];
                                    break;
                                }
                            }
                            b = true;
                            break;
                        }

                    }
                    if (!b)
                    {
                     */
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                   // }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
              dist[0]=0;
            color[0]=1;
            while (true)
            {
                int id = -1;
                for (int i = 0; i < n; i++)
                {
                    if(color[i]==1)
                        if (id == -1 || dist[id] > dist[i])
                        {
                            id = i;
                        }

                }
                if (id == -1) break;
                color[id] = 2;
                foreach (var i in lst[id])
                {
                    long nov = dist[id] + i.w;
                    if (color[i.k] == 0 || nov < dist[i.k])
                    {
                        color[i.k] = 1;
                        dist[i.k] = nov;
                        fr[i.k] = id;

                    }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
    }
    class Graph
    {
        public int k;
        public int w;
        public Graph(int kk, int ww)
        {
            k = kk;
            w = ww;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace code_20_c
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            int n = int.Parse(s.Substring(0, s.IndexOf(' ')));
            int m = int.Parse(s.Substring( s.IndexOf(' ')+1));
            List<Graph>[] lst = new List<Graph>[n];
            for (int i = 0; i < n; i++)
                lst[i] = new List<Graph>();
            int []mas = new int[3];
            Dictionary<string, int> dic = new Dictionary<string, int>();
            string str = "";
            for (int i = 0; i < m; i++)
            {
                mas = Console.ReadLine().Split(' ').Select(ss => int.Parse(ss)).ToArray();
                str = mas[0].ToString() + mas[1].ToString();

                if (mas[0] != mas[1])
                {
                    mas[0]--;
                    mas[1]--;
                    if (dic.ContainsKey(str) && dic[str] > mas[2])
                    {

                        foreach (var item in lst[mas[0]])
                        {
                            if (item.k == mas[1] && item.w > mas[2])
                            {
                                item.w = mas[2];
                                foreach (var it in lst[mas[1]])
                                {
                                    if (it.k == mas[0] && item.w > mas[2])
                                    {
                                        item.w = mas[2];
                                        break;
                                    }
                                }

                                break;
                            }

                        }
                    }
                    else
                    {
                        dic[str] = mas[2];
                        lst[mas[0]].Add(new Graph(mas[1], mas[2]));
                        lst[mas[1]].Add(new Graph(mas[0], mas[2]));
                    }
                }

            }
            int[] color = new int[n];
            long[] dist = new long[n];
            int[] fr = new int[n];
              dist[0]=0;
            color[0]=1;
            while (true)
            {
                int id = -1;
                for (int i = 0; i < n; i++)
                {
                    if(color[i]==1)
                        if (id == -1 || dist[id] > dist[i])
                        {
                            id = i;
                        }

                }
                if (id == -1) break;
                color[id] = 2;
                foreach (var i in lst[id])
                {
                    long nov = dist[id] + i.w;
                    if (color[i.k] == 0 || nov < dist[i.k])
                    {
                        color[i.k] = 1;
                        dist[i.k] = nov;
                        fr[i.k] = id;

                    }

                }

            }

            if (dist[n - 1] == 0)
            { Console.WriteLine(-1); }
            else
            {
                int k = fr[n - 1];
                List<int> ls = new List<int>();

                while (true)
                {
                    ls.Add(k);
                    if (k == 0) break;
                    k = fr[k];
                }
                for (int i =ls.Count-1; i >=0; i--)
                {
                    Console.Write(ls[i]+1+" ");
                }
                Console.WriteLine(n);
            }

        }
    }
    class Graph
    {
        public int k;
        public int w;
        public Graph(int kk, int ww)
        {
            k = kk;
            w = ww;
        }
    }
}