#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') {
      f = -1;
    }
    ch = getchar();
  }
  while (ch <= '9' && ch >= '0') {
    x *= 10;
    x += ch - '0';
    ch = getchar();
  }
  return x * f;
}
struct node {
  long long pos, w;
  inline bool operator<(const node &x) const { return w > x.w; }
};
struct EDEGE {
  long long nxt, to, w;
} e[500861];
long long cnt, n, m, fir[500861];
void add(long long x, long long y, long long z) {
  e[++cnt].nxt = fir[x];
  fir[x] = cnt;
  e[cnt].to = y;
  e[cnt].w = z;
}
priority_queue<node> q;
bool vis[500861];
long long dis[500861];
long long fa[500861];
void dj() {
  if (q.empty() || vis[n]) {
    return;
  }
  node xx = q.top();
  q.pop();
  long long x = xx.pos;
  while (vis[x]) {
    xx = q.top();
    q.pop();
    x = xx.pos;
  }
  vis[x] = 1;
  node temp;
  for (long long now = fir[x]; now; now = e[now].nxt) {
    if (dis[e[now].to] > dis[x] + e[now].w) {
      dis[e[now].to] = dis[x] + e[now].w;
      fa[e[now].to] = x;
    }
    if (!vis[e[now].to]) {
      temp.pos = e[now].to;
      temp.w = dis[e[now].to];
      q.push(temp);
    }
  }
  dj();
}
long long ans[500861];
int main() {
  scanf("%lld%lld", &n, &m);
  long long x, y, z;
  for (long long i = 1; i <= m; i++) {
    scanf("%lld%lld%lld", &x, &y, &z);
    add(x, y, z);
    add(y, x, z);
  }
  for (long long i = 1; i <= n; i++) {
    dis[i] = 20000000000000000;
  }
  dis[1] = 0;
  node temp;
  temp.pos = 1, temp.w = 0;
  q.push(temp);
  dj();
  if (!vis[n]) {
    printf("-1");
    return 0;
  }
  int qi = 0;
  for (int i = n; i; i = fa[i]) {
    ans[++qi] = i;
  }
  for (int i = qi; i >= 1; i--) {
    printf("%lld ", ans[i]);
  }
  return 0;
}
