#include <bits/stdc++.h>
using namespace std;
struct edge {
  int to, dis, next;
} ed[200001];
struct path {
  int point, dis;
};
int v[100001], cnt = 0;
long long dis[100001];
bool vis[100001];
stack<int> s;
bool operator<(const path &a, const path &b) { return a.dis > b.dis; }
priority_queue<path> heap;
inline int read() {
  char c = getchar();
  int x = 0;
  while (c < 48) c = getchar();
  while (c > 47) x = (x << 3) + (x << 1) + c - 48, c = getchar();
  return x;
}
void add_edge(int s, int e, int d) {
  ed[++cnt] = (edge){e, d, v[s]};
  v[s] = cnt;
}
void Dijkstra() {
  heap.push((path){1, 0});
  vis[1] = true;
  while (!heap.empty()) {
    path p = heap.top();
    heap.pop();
    int now = p.point;
    for (int e = v[now]; e; e = ed[e].next) {
      int to = ed[e].to;
      if (dis[to] > dis[now] + ed[e].dis) {
        dis[to] = dis[now] + ed[e].dis;
        if (!vis[to]) vis[to] = true;
        heap.push((path){to, ed[e].dis});
      }
    }
  }
}
void dfs(int now) {
  for (int e = v[now]; e; e = ed[e].next) {
    int to = ed[e].to;
    if (dis[to] == dis[now] - ed[e].dis) {
      s.push(to);
      dfs(to);
      break;
    }
  }
}
int main() {
  int n = read(), m = read();
  for (int i = 1; i <= m; i++) {
    int s = read(), e = read(), d = read();
    add_edge(s, e, d);
    add_edge(e, s, d);
  }
  for (int i = 2; i <= n; i++) dis[i] = 1e15;
  Dijkstra();
  if (dis[n] == 1e15) {
    cout << -1;
    return 0;
  }
  s.push(n);
  dfs(n);
  while (!s.empty()) printf("%d ", s.top()), s.pop();
  return 0;
}
