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

namespace mac_practice
{
    class Program
    {
        #region Декартово дерево
        public class CTree<T> where T : IComparable
        {
            static private Random _random = new Random(System.DateTime.Now.Millisecond);
            Node _root;

            public int Count
            {
                get
                {
                    return Node.GetCnt(_root);
                }
            }
            private class Node
            {
                public int cnt;
                public int priority;
                public T key;
                public Node left, right;
                static public int GetCnt(Node node)
                {
                    return node == null ? 0 : node.cnt;
                }
                static public void Update(Node node)
                {
                    if (node != null)
                    {
                        node.cnt = GetCnt(node.left) + GetCnt(node.right) + 1;
                    }
                }
                public Node(int cnt, int priority, T key, Node left, Node right)
                {
                    this.cnt = cnt;
                    this.priority = priority;
                    this.key = key;
                    this.left = left;
                    this.right = right;
                }
                public Node(T key)
                {
                    this.cnt = 1;
                    this.priority = _random.Next();
                    this.key = key;
                    this.left = null;
                    this.right = null;
                }
            }

            public CTree()
            {
                _root = null;
            }
            static Node splitLeft, splitRight;
            private Node Merge(Node left, Node right)
            {
                if (left == null)
                    return right;
                if (right == null)
                    return left;
                Node res;
                if (left.priority < right.priority)
                {
                    right.left = Merge(left, right.left);
                    res = right;
                }
                else
                {
                    left.right = Merge(left.right, right);
                    res = left;
                }
                Node.Update(res);
                return res;
            }
            private void Split(Node tree, T byValue)
            {
                if (tree == null)
                {
                    splitLeft = splitRight = null;
                    return;
                }
                int cmp = tree.key.CompareTo(byValue);
                if (cmp <= 0)
                {
                    Split(tree.right, byValue);
                    tree.right = splitLeft;
                    splitLeft = tree;
                }
                else
                {
                    Split(tree.left, byValue);
                    tree.left = splitRight;
                    splitRight = tree;
                }
                Node.Update(splitLeft);
                Node.Update(splitRight);
            }
            public bool Add(T key)
            {
                if (Contains(key))
                    return false;
                Split(_root, key);
                _root = Merge(splitLeft, new Node(key));
                _root = Merge(_root, splitRight);
                return true;
            }

            public bool Contains(T key)
            {
                return Contains(_root, key);
            }
            private bool Contains(Node root, T value)
            {
                if (root == null)
                    return false;
                int cmp = root.key.CompareTo(value);
                if (cmp == 0)
                    return true;
                if (cmp < 0)
                    return Contains(root.right, value);
                return Contains(root.left, value);
            }
            public bool Remove(T key)
            {
                if (!Contains(key))
                    return false;
                Remove(ref _root, key);
                return true;
            }

            private void Remove(ref Node root, T key)
            {
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                {
                    root = Merge(root.left, root.right);
                }
                else if (cmp < 0)
                {
                    Remove(ref root.right, key);
                }
                else
                {
                    Remove(ref root.left, key);
                }
                Node.Update(root);
            }
            public override string ToString()
            {
                StringBuilder sbl = new StringBuilder();
                dfs(_root, sbl);
                String ans = sbl.ToString();
                return "[" + ans.Substring(0, ans.Length - 2) + "]";
            }

            private void dfs(Node root, StringBuilder sbl)
            {
                if (root == null)
                {
                    return;
                }
                dfs(root.left, sbl);
                sbl.Append(root.key);
                sbl.Append(", ");
                dfs(root.right, sbl);
            }
            public bool EqualOrNext(T by, out T res)
            {
                return EqualOrNext(_root, by, out res);
            }

            private bool EqualOrNext(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp < 0)
                    return EqualOrNext(root.right, by, out res);
                bool left = EqualOrNext(root.left, by, out res);
                if (!left)
                    res = root.key;
                return true;

            }
            public bool Next(T by, out T res)
            {
                return Next(_root, by, out res);
            }

            private bool Next(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp <= 0)
                {
                    return Next(root.right, by, out res);
                }
                else
                {
                    bool left = Next(root.left, by, out res);
                    if (!left)
                        res = root.key;
                    return true;
                }
            }
            public bool EqualOrPrevious(T by, out T res)
            {
                return EqualOrPrevious(_root, by, out res);
            }

            private bool EqualOrPrevious(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp > 0)
                {
                    return EqualOrPrevious(root.left, by, out res);
                }
                else
                {
                    bool right = EqualOrPrevious(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }

            }
            public bool Previous(T by, out T res)
            {
                return Previous(_root, by, out res);
            }

            private bool Previous(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp >= 0)
                    return Previous(root.left, by, out res);
                else
                {
                    bool right = Previous(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }
            }
            public bool Min(out T res)
            {
                return Min(_root, out res);
            }

            private bool Min(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Min(root.left, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }
            public bool Max(out T res)
            {
                return Max(_root, out res);
            }

            private bool Max(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Max(root.right, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }

        }
        #endregion Декартово дерево
        static void Main(string[] args)
        {
            Program p = new Program();
            p.solve();
        }

        class FastScanner
        {
            private String[] tokens;
            private int pointer;
            public FastScanner()
            {
                pointer = 0;
                tokens = null;
            }
            public long nextLong()
            {
                return long.Parse(nextToken());
            }
            public int nextInt()
            {
                return int.Parse(nextToken());
            }
            public string nextToken()
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = System.Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
        }

        class Edge
        {
            public int to, weight;
            public Edge(int to, int weight)
            {
                this.to = to;
                this.weight = weight;
            }
        }
        class Item : IComparable
        {
            public int v;
            public Item(int v)
            {
                this.v = v;
            }
            public int CompareTo(object obj)
            {
                Item other = (Item)obj;
                long d = dist[v] - dist[other.v];
                if (d == 0) return v - other.v;
                if (d < 0) return -1;
                return 1;
            }
        }
        List<Edge>[] graph;
        static long[] dist;
        int[] prev;

        private void solve()
        {
            FastScanner input = new FastScanner();
            int n = input.nextInt();
            int m = input.nextInt();
            graph = new List<Edge>[n];
            for (int i = 0; i < n; ++i)
                graph[i] = new List<Edge>();
            dist = new long[n];
            prev = new int[n];
            for (int i = 0; i < m; i++)
            {
                int x = input.nextInt() - 1;
                int y = input.nextInt() - 1;
                int w = input.nextInt() - 1;
                graph[x].Add(new Edge(y, w));
                graph[y].Add(new Edge(x, w));
            }
            CTree<Item> queue = new CTree<Item>();
            for (int i = 0; i < n; i++)
            {
                dist[i] = long.MaxValue / 2;
            }
            dist[0] = 0;
            queue.Add(new Item(0));
            while (queue.Count > 0)
            {
                Item min;
                queue.Min(out min);
                queue.Remove(min);
                int v = min.v;

                foreach (var edge in graph[v])
                {
                    if (dist[edge.to] > dist[v] + edge.weight)
                    {
                        queue.Remove(new Item(edge.to));
                        dist[edge.to] = dist[v] + edge.weight;
                        prev[edge.to] = v;
                        queue.Add(new Item(edge.to));
                    }
                }
            }
            if (dist[n - 1] == long.MaxValue / 2)
            {
                Console.WriteLine(-1);
            }
            else
            {
                List<int> path = new List<int>();
                for (int cur = n - 1; cur != 0; cur = prev[cur])
                    path.Add(cur + 1);
                path.Add(1);
                path.Reverse();
                foreach(var x in path)
                    Console.Write(x + " ");
            }
        }
    }

}


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

namespace mac_practice
{
    class Program
    {
        #region Декартово дерево
        public class CTree<T> where T : IComparable
        {
            static private Random _random = new Random(System.DateTime.Now.Millisecond);
            Node _root;

            public int Count
            {
                get
                {
                    return Node.GetCnt(_root);
                }
            }
            private class Node
            {
                public int cnt;
                public int priority;
                public T key;
                public Node left, right;
                static public int GetCnt(Node node)
                {
                    return node == null ? 0 : node.cnt;
                }
                static public void Update(Node node)
                {
                    if (node != null)
                    {
                        node.cnt = GetCnt(node.left) + GetCnt(node.right) + 1;
                    }
                }
                public Node(int cnt, int priority, T key, Node left, Node right)
                {
                    this.cnt = cnt;
                    this.priority = priority;
                    this.key = key;
                    this.left = left;
                    this.right = right;
                }
                public Node(T key)
                {
                    this.cnt = 1;
                    this.priority = _random.Next();
                    this.key = key;
                    this.left = null;
                    this.right = null;
                }
            }

            public CTree()
            {
                _root = null;
            }
            static Node splitLeft, splitRight;
            private Node Merge(Node left, Node right)
            {
                if (left == null)
                    return right;
                if (right == null)
                    return left;
                Node res;
                if (left.priority < right.priority)
                {
                    right.left = Merge(left, right.left);
                    res = right;
                }
                else
                {
                    left.right = Merge(left.right, right);
                    res = left;
                }
                Node.Update(res);
                return res;
            }
            private void Split(Node tree, T byValue)
            {
                if (tree == null)
                {
                    splitLeft = splitRight = null;
                    return;
                }
                int cmp = tree.key.CompareTo(byValue);
                if (cmp <= 0)
                {
                    Split(tree.right, byValue);
                    tree.right = splitLeft;
                    splitLeft = tree;
                }
                else
                {
                    Split(tree.left, byValue);
                    tree.left = splitRight;
                    splitRight = tree;
                }
                Node.Update(splitLeft);
                Node.Update(splitRight);
            }
            public bool Add(T key)
            {
                if (Contains(key))
                    return false;
                Split(_root, key);
                _root = Merge(splitLeft, new Node(key));
                _root = Merge(_root, splitRight);
                return true;
            }

            public bool Contains(T key)
            {
                return Contains(_root, key);
            }
            private bool Contains(Node root, T value)
            {
                if (root == null)
                    return false;
                int cmp = root.key.CompareTo(value);
                if (cmp == 0)
                    return true;
                if (cmp < 0)
                    return Contains(root.right, value);
                return Contains(root.left, value);
            }
            public bool Remove(T key)
            {
                if (!Contains(key))
                    return false;
                Remove(ref _root, key);
                return true;
            }

            private void Remove(ref Node root, T key)
            {
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                {
                    root = Merge(root.left, root.right);
                }
                else if (cmp < 0)
                {
                    Remove(ref root.right, key);
                }
                else
                {
                    Remove(ref root.left, key);
                }
                Node.Update(root);
            }
            public override string ToString()
            {
                StringBuilder sbl = new StringBuilder();
                dfs(_root, sbl);
                String ans = sbl.ToString();
                return "[" + ans.Substring(0, ans.Length - 2) + "]";
            }

            private void dfs(Node root, StringBuilder sbl)
            {
                if (root == null)
                {
                    return;
                }
                dfs(root.left, sbl);
                sbl.Append(root.key);
                sbl.Append(", ");
                dfs(root.right, sbl);
            }
            public bool EqualOrNext(T by, out T res)
            {
                return EqualOrNext(_root, by, out res);
            }

            private bool EqualOrNext(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp < 0)
                    return EqualOrNext(root.right, by, out res);
                bool left = EqualOrNext(root.left, by, out res);
                if (!left)
                    res = root.key;
                return true;

            }
            public bool Next(T by, out T res)
            {
                return Next(_root, by, out res);
            }

            private bool Next(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp <= 0)
                {
                    return Next(root.right, by, out res);
                }
                else
                {
                    bool left = Next(root.left, by, out res);
                    if (!left)
                        res = root.key;
                    return true;
                }
            }
            public bool EqualOrPrevious(T by, out T res)
            {
                return EqualOrPrevious(_root, by, out res);
            }

            private bool EqualOrPrevious(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp > 0)
                {
                    return EqualOrPrevious(root.left, by, out res);
                }
                else
                {
                    bool right = EqualOrPrevious(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }

            }
            public bool Previous(T by, out T res)
            {
                return Previous(_root, by, out res);
            }

            private bool Previous(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp >= 0)
                    return Previous(root.left, by, out res);
                else
                {
                    bool right = Previous(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }
            }
            public bool Min(out T res)
            {
                return Min(_root, out res);
            }

            private bool Min(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Min(root.left, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }
            public bool Max(out T res)
            {
                return Max(_root, out res);
            }

            private bool Max(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Max(root.right, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }

        }
        #endregion Декартово дерево
        static void Main(string[] args)
        {
            Program p = new Program();
            p.solve();
        }

        class FastScanner
        {
            private String[] tokens;
            private int pointer;
            public FastScanner()
            {
                pointer = 0;
                tokens = null;
            }
            public long nextLong()
            {
                return long.Parse(nextToken());
            }
            public int nextInt()
            {
                return int.Parse(nextToken());
            }
            public string nextToken()
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = System.Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
        }

        class Edge
        {
            public int to, weight;
            public Edge(int to, int weight)
            {
                this.to = to;
                this.weight = weight;
            }
        }
        class Item : IComparable
        {
            public int v;
            public Item(int v)
            {
                this.v = v;
            }
            public int CompareTo(object obj)
            {
                Item other = (Item)obj;
                long d = dist[v] - dist[other.v];
                if (d == 0) return 0;
                if (d < 0) return -1;
                return 1;
            }
        }
        List<Edge>[] graph;
        static long[] dist;
        int[] prev;

        private void solve()
        {
            FastScanner input = new FastScanner();
            int n = input.nextInt();
            int m = input.nextInt();
            graph = new List<Edge>[n];
            for (int i = 0; i < n; ++i)
                graph[i] = new List<Edge>();
            dist = new long[n];
            prev = new int[n];
            for (int i = 0; i < m; i++)
            {
                int x = input.nextInt() - 1;
                int y = input.nextInt() - 1;
                int w = input.nextInt() - 1;
                graph[x].Add(new Edge(y, w));
                graph[y].Add(new Edge(x, w));
            }
            CTree<Item> queue = new CTree<Item>();
            for (int i = 0; i < n; i++)
            {
                dist[i] = long.MaxValue / 2;
            }
            dist[0] = 0;
            queue.Add(new Item(0));
            while (queue.Count > 0)
            {
                Item min;
                queue.Min(out min);
                queue.Remove(min);
                int v = min.v;

                foreach (var edge in graph[v])
                {
                    if (dist[edge.to] > dist[v] + edge.weight)
                    {
                        queue.Remove(new Item(edge.to));
                        dist[edge.to] = dist[v] + edge.weight;
                        prev[edge.to] = v;
                        queue.Add(new Item(edge.to));
                    }
                }
            }
            if (dist[n - 1] == long.MaxValue / 2)
            {
                Console.WriteLine(-1);
            }
            else
            {
                List<int> path = new List<int>();
                for (int cur = n - 1; cur != 0; cur = prev[cur])
                    path.Add(cur + 1);
                path.Add(1);
                path.Reverse();
                foreach(var x in path)
                    Console.Write(x + " ");
            }
        }
    }

}


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

namespace mac_practice
{
    class Program
    {
        #region Декартово дерево
        public class CTree<T> where T : IComparable
        {
            static private Random _random = new Random(System.DateTime.Now.Millisecond);
            Node _root;

            public int Count
            {
                get
                {
                    return Node.GetCnt(_root);
                }
            }
            private class Node
            {
                public int cnt;
                public int priority;
                public T key;
                public Node left, right;
                static public int GetCnt(Node node)
                {
                    return node == null ? 0 : node.cnt;
                }
                static public void Update(Node node)
                {
                    if (node != null)
                    {
                        node.cnt = GetCnt(node.left) + GetCnt(node.right) + 1;
                    }
                }
                public Node(int cnt, int priority, T key, Node left, Node right)
                {
                    this.cnt = cnt;
                    this.priority = priority;
                    this.key = key;
                    this.left = left;
                    this.right = right;
                }
                public Node(T key)
                {
                    this.cnt = 1;
                    this.priority = _random.Next();
                    this.key = key;
                    this.left = null;
                    this.right = null;
                }
            }

            public CTree()
            {
                _root = null;
            }
            static Node splitLeft, splitRight;
            private Node Merge(Node left, Node right)
            {
                if (left == null)
                    return right;
                if (right == null)
                    return left;
                Node res;
                if (left.priority < right.priority)
                {
                    right.left = Merge(left, right.left);
                    res = right;
                }
                else
                {
                    left.right = Merge(left.right, right);
                    res = left;
                }
                Node.Update(res);
                return res;
            }
            private void Split(Node tree, T byValue)
            {
                if (tree == null)
                {
                    splitLeft = splitRight = null;
                    return;
                }
                int cmp = tree.key.CompareTo(byValue);
                if (cmp <= 0)
                {
                    Split(tree.right, byValue);
                    tree.right = splitLeft;
                    splitLeft = tree;
                }
                else
                {
                    Split(tree.left, byValue);
                    tree.left = splitRight;
                    splitRight = tree;
                }
                Node.Update(splitLeft);
                Node.Update(splitRight);
            }
            public bool Add(T key)
            {
                if (Contains(key))
                    return false;
                Split(_root, key);
                _root = Merge(splitLeft, new Node(key));
                _root = Merge(_root, splitRight);
                return true;
            }

            public bool Contains(T key)
            {
                return Contains(_root, key);
            }
            private bool Contains(Node root, T key)
            {
                if (root == null)
                    return false;
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                    return true;
                if (cmp < 0)
                    return Contains(root.right, key);
                return Contains(root.left, key);
            }
            public bool Remove(T key)
            {
                if (!Contains(key))
                    return false;
                Remove(ref _root, key);
                return true;
            }

            private void Remove(ref Node root, T key)
            {
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                {
                    root = Merge(root.left, root.right);
                }
                else if (cmp < 0)
                {
                    Remove(ref root.right, key);
                }
                else
                {
                    Remove(ref root.left, key);
                }
                Node.Update(root);
            }
            public override string ToString()
            {
                StringBuilder sbl = new StringBuilder();
                dfs(_root, sbl);
                String ans = sbl.ToString();
                return "[" + ans.Substring(0, ans.Length - 2) + "]";
            }

            private void dfs(Node root, StringBuilder sbl)
            {
                if (root == null)
                {
                    return;
                }
                dfs(root.left, sbl);
                sbl.Append(root.key);
                sbl.Append(", ");
                dfs(root.right, sbl);
            }
            public bool EqualOrNext(T by, out T res)
            {
                return EqualOrNext(_root, by, out res);
            }

            private bool EqualOrNext(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp < 0)
                    return EqualOrNext(root.right, by, out res);
                bool left = EqualOrNext(root.left, by, out res);
                if (!left)
                    res = root.key;
                return true;

            }
            public bool Next(T by, out T res)
            {
                return Next(_root, by, out res);
            }

            private bool Next(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp <= 0)
                {
                    return Next(root.right, by, out res);
                }
                else
                {
                    bool left = Next(root.left, by, out res);
                    if (!left)
                        res = root.key;
                    return true;
                }
            }
            public bool EqualOrPrevious(T by, out T res)
            {
                return EqualOrPrevious(_root, by, out res);
            }

            private bool EqualOrPrevious(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp > 0)
                {
                    return EqualOrPrevious(root.left, by, out res);
                }
                else
                {
                    bool right = EqualOrPrevious(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }

            }
            public bool Previous(T by, out T res)
            {
                return Previous(_root, by, out res);
            }

            private bool Previous(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp >= 0)
                    return Previous(root.left, by, out res);
                else
                {
                    bool right = Previous(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }
            }
            public bool Min(out T res)
            {
                return Min(_root, out res);
            }

            private bool Min(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Min(root.left, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }
            public bool Max(out T res)
            {
                return Max(_root, out res);
            }

            private bool Max(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Max(root.right, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }

        }
        #endregion Декартово дерево
        static void Main(string[] args)
        {
            Program p = new Program();
            p.solve();
        }

        class FastScanner
        {
            private String[] tokens;
            private int pointer;
            public FastScanner()
            {
                pointer = 0;
                tokens = null;
            }
            public long nextLong()
            {
                return long.Parse(nextToken());
            }
            public int nextInt()
            {
                return int.Parse(nextToken());
            }
            public string nextToken()
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = System.Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
        }

        class Edge
        {
            public int to, weight;
            public Edge(int to, int weight)
            {
                this.to = to;
                this.weight = weight;
            }
        }
        class Item : IComparable
        {
            public int v;
            public Item(int v)
            {
                this.v = v;
            }
            public int CompareTo(object obj)
            {
                Item other = (Item)obj;
                long d = dist[v] - dist[other.v];
                if (d == 0) return v - other.v;
                if (d < 0) return -1;
                return 1;
            }
        }
        List<Edge>[] graph;
        static long[] dist;
        int[] prev;

        private void solve()
        {
            FastScanner input = new FastScanner();
            int n = input.nextInt();
            int m = input.nextInt();
            graph = new List<Edge>[n];
            for (int i = 0; i < n; ++i)
                graph[i] = new List<Edge>();
            dist = new long[n];
            prev = new int[n];
            for (int i = 0; i < m; i++)
            {
                int x = input.nextInt() - 1;
                int y = input.nextInt() - 1;
                int w = input.nextInt() ;
                graph[x].Add(new Edge(y, w));
                graph[y].Add(new Edge(x, w));
            }
            CTree<Item> queue = new CTree<Item>();
            for (int i = 0; i < n; i++)
            {
                dist[i] = long.MaxValue / 2;
            }
            dist[0] = 0;
            queue.Add(new Item(0));
            while (queue.Count > 0)
            {
                Item min;
                queue.Min(out min);
                queue.Remove(min);
                int v = min.v;

                foreach (var edge in graph[v])
                {
                    if (dist[edge.to] > dist[v] + edge.weight)
                    {
                        queue.Remove(new Item(edge.to));
                        dist[edge.to] = dist[v] + edge.weight;
                        prev[edge.to] = v;
                        queue.Add(new Item(edge.to));
                    }
                }
            }
            if (dist[n - 1] == long.MaxValue / 2)
            {
                Console.WriteLine(-1);
            }
            else
            {
                List<int> path = new List<int>();
                for (int cur = n - 1; cur != 0; cur = prev[cur])
                    path.Add(cur + 1);
                path.Add(1);
                path.Reverse();
                foreach(var x in path)
                    Console.Write(x + " ");
            }
        }
    }

}


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

namespace mac_practice
{
    class Program
    {
        #region Декартово дерево
        public class CTree<T> where T : IComparable
        {
            static private Random _random = new Random(System.DateTime.Now.Millisecond);
            Node _root;

            public int Count
            {
                get
                {
                    return Node.GetCnt(_root);
                }
            }
            private class Node
            {
                public int cnt;
                public int priority;
                public T key;
                public Node left, right;
                static public int GetCnt(Node node)
                {
                    return node == null ? 0 : node.cnt;
                }
                static public void Update(Node node)
                {
                    if (node != null)
                    {
                        node.cnt = GetCnt(node.left) + GetCnt(node.right) + 1;
                    }
                }
                public Node(int cnt, int priority, T key, Node left, Node right)
                {
                    this.cnt = cnt;
                    this.priority = priority;
                    this.key = key;
                    this.left = left;
                    this.right = right;
                }
                public Node(T key)
                {
                    this.cnt = 1;
                    this.priority = _random.Next();
                    this.key = key;
                    this.left = null;
                    this.right = null;
                }
            }

            public CTree()
            {
                _root = null;
            }
            static Node splitLeft, splitRight;
            private Node Merge(Node left, Node right)
            {
                if (left == null)
                    return right;
                if (right == null)
                    return left;
                Node res;
                if (left.priority < right.priority)
                {
                    right.left = Merge(left, right.left);
                    res = right;
                }
                else
                {
                    left.right = Merge(left.right, right);
                    res = left;
                }
                Node.Update(res);
                return res;
            }
            private void Split(Node tree, T byValue)
            {
                if (tree == null)
                {
                    splitLeft = splitRight = null;
                    return;
                }
                int cmp = tree.key.CompareTo(byValue);
                if (cmp <= 0)
                {
                    Split(tree.right, byValue);
                    tree.right = splitLeft;
                    splitLeft = tree;
                }
                else
                {
                    Split(tree.left, byValue);
                    tree.left = splitRight;
                    splitRight = tree;
                }
                Node.Update(splitLeft);
                Node.Update(splitRight);
            }
            public bool Add(T key)
            {
                if (Contains(key))
                    return false;
                Add(ref _root, new Node(key));
                return true;
                /*Split(_root, key);
                _root = Merge(splitLeft, new Node(key));
                _root = Merge(_root, splitRight);
                return true;*/
            }

            private void Add(ref Node _root, Node node)
            {
                if (_root == null)
                    _root = node;
                else if (node.priority < _root.priority)
                {
                    Split(_root, node.key);
                    node.left = splitLeft;
                    node.right = splitRight;
                    _root = node;
                    Node.Update(_root);
                }
                else
                {
                    if (node.key.CompareTo(_root.key) < 0)
                        Add(ref _root.left, node);
                    else
                        Add(ref _root.right, node);
                }
            }

            public bool Contains(T key)
            {
                return Contains(_root, key);
            }
            private bool Contains(Node root, T key)
            {
                if (root == null)
                    return false;
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                    return true;
                if (cmp < 0)
                    return Contains(root.right, key);
                return Contains(root.left, key);
            }
            public bool Remove(T key)
            {
                if (!Contains(key))
                    return false;
                Remove(ref _root, key);
                return true;
            }

            private void Remove(ref Node root, T key)
            {
                int cmp = root.key.CompareTo(key);
                if (cmp == 0)
                {
                    root = Merge(root.left, root.right);
                }
                else if (cmp < 0)
                {
                    Remove(ref root.right, key);
                }
                else
                {
                    Remove(ref root.left, key);
                }
                Node.Update(root);
            }
            public override string ToString()
            {
                StringBuilder sbl = new StringBuilder();
                dfs(_root, sbl);
                String ans = sbl.ToString();
                return "[" + ans.Substring(0, ans.Length - 2) + "]";
            }

            private void dfs(Node root, StringBuilder sbl)
            {
                if (root == null)
                {
                    return;
                }
                dfs(root.left, sbl);
                sbl.Append(root.key);
                sbl.Append(", ");
                dfs(root.right, sbl);
            }
            public bool EqualOrNext(T by, out T res)
            {
                return EqualOrNext(_root, by, out res);
            }

            private bool EqualOrNext(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp < 0)
                    return EqualOrNext(root.right, by, out res);
                bool left = EqualOrNext(root.left, by, out res);
                if (!left)
                    res = root.key;
                return true;

            }
            public bool Next(T by, out T res)
            {
                return Next(_root, by, out res);
            }

            private bool Next(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp <= 0)
                {
                    return Next(root.right, by, out res);
                }
                else
                {
                    bool left = Next(root.left, by, out res);
                    if (!left)
                        res = root.key;
                    return true;
                }
            }
            public bool EqualOrPrevious(T by, out T res)
            {
                return EqualOrPrevious(_root, by, out res);
            }

            private bool EqualOrPrevious(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp == 0)
                {
                    res = root.key;
                    return true;
                }
                if (cmp > 0)
                {
                    return EqualOrPrevious(root.left, by, out res);
                }
                else
                {
                    bool right = EqualOrPrevious(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }

            }
            public bool Previous(T by, out T res)
            {
                return Previous(_root, by, out res);
            }

            private bool Previous(Node root, T by, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                int cmp = root.key.CompareTo(by);
                if (cmp >= 0)
                    return Previous(root.left, by, out res);
                else
                {
                    bool right = Previous(root.right, by, out res);
                    if (!right)
                        res = root.key;
                    return true;
                }
            }
            public bool Min(out T res)
            {
                return Min(_root, out res);
            }

            private bool Min(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Min(root.left, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }
            public bool Max(out T res)
            {
                return Max(_root, out res);
            }

            private bool Max(Node root, out T res)
            {
                if (root == null)
                {
                    res = default(T);
                    return false;
                }
                bool now = Max(root.right, out res);
                if (now)
                    return true;
                res = root.key;
                return true;
            }

        }
        #endregion Декартово дерево
        static void Main(string[] args)
        {
            Program p = new Program();
            p.solve();
        }

        class FastScanner
        {
            private String[] tokens;
            private int pointer;
            public FastScanner()
            {
                pointer = 0;
                tokens = null;
            }
            public long nextLong()
            {
                return long.Parse(nextToken());
            }
            public int nextInt()
            {
                return int.Parse(nextToken());
            }
            public string nextToken()
            {
                while (tokens == null || pointer >= tokens.Length)
                {
                    tokens = System.Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    pointer = 0;
                }
                return tokens[pointer++];
            }
        }

        class Edge
        {
            public int to, weight;
            public Edge(int to, int weight)
            {
                this.to = to;
                this.weight = weight;
            }
        }
        class Item : IComparable
        {
            public int v;
            public Item(int v)
            {
                this.v = v;
            }
            public int CompareTo(object obj)
            {
                Item other = (Item)obj;
                long d = dist[v] - dist[other.v];
                if (d == 0) return v - other.v;
                if (d < 0) return -1;
                return 1;
            }
        }
        List<Edge>[] graph;
        static long[] dist;
        int[] prev;

        private void solve()
        {
            FastScanner input = new FastScanner();
            int n = input.nextInt();
            int m = input.nextInt();
            graph = new List<Edge>[n];
            for (int i = 0; i < n; ++i)
                graph[i] = new List<Edge>();
            dist = new long[n];
            prev = new int[n];
            for (int i = 0; i < m; i++)
            {
                int x = input.nextInt() - 1;
                int y = input.nextInt() - 1;
                int w = input.nextInt() ;
                graph[x].Add(new Edge(y, w));
                graph[y].Add(new Edge(x, w));
            }
            CTree<Item> queue = new CTree<Item>();
            for (int i = 0; i < n; i++)
            {
                dist[i] = long.MaxValue / 2;
            }
            dist[0] = 0;
            queue.Add(new Item(0));
            while (queue.Count > 0)
            {
                Item min;
                queue.Min(out min);
                queue.Remove(min);
                int v = min.v;

                foreach (var edge in graph[v])
                {
                    if (dist[edge.to] > dist[v] + edge.weight)
                    {
                        queue.Remove(new Item(edge.to));
                        dist[edge.to] = dist[v] + edge.weight;
                        prev[edge.to] = v;
                        queue.Add(new Item(edge.to));
                    }
                }
            }
            if (dist[n - 1] == long.MaxValue / 2)
            {
                Console.WriteLine(-1);
            }
            else
            {
                List<int> path = new List<int>();
                for (int cur = n - 1; cur != 0; cur = prev[cur])
                    path.Add(cur + 1);
                path.Add(1);
                path.Reverse();
                foreach(var x in path)
                    Console.Write(x + " ");
            }
        }
    }

}


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
                    {
                        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 Edge : IComparable<Edge>
    {
        public int From { get; set; }
        public int To { get; set; }
        public int Distance { get; set; }
        public List<int> Path { get; set; }

        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()
        {
            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(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
                    {
                        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.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
                    {
                        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.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(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
                    {
                        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 Edge : IComparable<Edge>
    {
        public int From { get; set; }
        public int To { get; set; }
        public int Distance { get; set; }
        public List<int> Path { get; set; }

        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(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];
                for (var i = 0; i < distanceToTravels.Length; i++)
                {
                    distanceToTravels[i] = 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 Edge : IComparable<Edge>
    {
        public int From { get; set; }
        public int To { get; set; }
        public int Distance { get; set; }
        public List<int> Path { get; set; }

        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()
        {
            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 System.Text;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var n = NextInt();
            var m = NextInt();
            var list = new List<Edge>[n + 1];
            for (var i = 1; i <= n; i++)
            {
                list[i] = new List<Edge>();
            }
            for (var i = 0; i < m; i++)
            {

                var a = NextInt();
                var b = NextInt();
                var w = NextInt();
                list[a].Add(new Edge(b, w));
                list[b].Add(new Edge(a, w));
            }

            var visited = new bool[n + 1];
            var shortestPaths = Enumerable.Range(0, n + 1).Select(a => long.MaxValue).ToArray();
            var from = new int[n + 1];
            shortestPaths[1] = 0;

            var queue = new PriorityQueue<Edge>();
            queue.Enqueue(new Edge(1, 0));
            while (queue.Count() > 0)
            {
                var edge = queue.Dequeue();
                var vertex = edge.Vertex;
                if (visited[vertex])
                {
                    continue;
                }

                visited[vertex] = true;

                foreach (var nextEdge in list[vertex])
                {
                    var newVertex = nextEdge.Vertex;
                    var distance = shortestPaths[vertex] + nextEdge.Value;
                    if (distance >= shortestPaths[newVertex]) continue;
                    from[newVertex] = vertex;
                    shortestPaths[newVertex] = distance;
                    queue.Enqueue(new Edge(newVertex, shortestPaths[newVertex]));
                }
            }
            if (from[n] == 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var path = new List<int>();
            var value = n;
            while (value != 1)
            {
                path.Add(value);
                value = from[value];
            }
            path.Add(1);

            var result = new StringBuilder();
            for (var i = path.Count - 1; i >= 0; i--)
            {
                result.Append(path[i] + " ");
            }
            result.Remove(result.Length - 1, 1);
            Console.WriteLine(result);

            Console.ReadLine();
        }

        private static int _sIndex;
        private static List<string> _sTokens;
        private static string Next()
        {
            while (_sTokens == null || _sIndex == _sTokens.Count)
            {
                _sTokens = Console.ReadLine()?.Split(' ').ToList();
                _sIndex = 0;
            }
            return _sTokens[_sIndex++];
        }
        private static int NextInt()
        {
            return int.Parse(Next());
        }
    }

    internal class Edge : IComparable<Edge>
    {
        public int Vertex { get; set; }
        public long Value { get; set; }
        public Edge(int vertex, long value)
        {
            this.Vertex = vertex;
            this.Value = value;
        }

        public int CompareTo(Edge o)
        {
            return Value.CompareTo(o.Value);
        }
    }
    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 Peek()
        {
            return _items[0];
        }

        public T Dequeue()
        {
            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;
        }
    }
}