#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct e
{
int next, to, weight;
}edge[500005];
int cnt = 0, head[10005], n, m, vis[100005], p[100005], path[100005], tot;
ll dis[100005], mx = 0x7FFFFFFF;
struct node
{
int num;
ll dis;
bool operator <(const node& no) const
{
return no.dis < dis;
}
};
priority_queue<node> q;
void add(int x, int y, ll w)
{
cnt++;
edge[cnt].weight = w;
edge[cnt].to = y;
edge[cnt].next = head[x];
head[x] = cnt;
}
void inti()
{
for (int i = 1; i <= n; i++)
dis[i] = mx;
dis[1] = 0;
}
void dij()
{
q.push(node{ 1,0 });
while (!q.empty())
{
node tmp = q.top();
q.pop();
int x = tmp.num;
if (vis[x])
continue;
vis[x] = 1;
for (int i = head[x]; i; i = edge[i].next)
{
int y = edge[i].to;
if (dis[y] > dis[x] + edge[i].weight&&!vis[y])
{
dis[y] = dis[x] + edge[i].weight;
p[y] = x;
q.push(node{ y, dis[y] });
}
}
}
}
void print()
{
if (dis[n]==mx)
{
cout << -1;
return;
}
else
{
for (int i = n; p[i]; i = p[i])
path[++tot] = i;
cout << 1 << " ";
for (int i = tot; i >=1; i--)
cout << path[i] << " ";
return;
}
}
int main()
{
cin >> n >> m;
while (m--)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
inti();
dij();
print();
return 0;
}