#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int INF = 0x3f3f3f3f;
const double EPS = 1e-10;
int inline in() {
  int x = 0, c;
  for (; (unsigned)((c = getchar()) - '0') >= 10;) {
    if (c == '-') return -in();
    if (!~c) throw ~0;
  }
  do {
    x = (x << 3) + (x << 1) + (c - '0');
  } while ((unsigned)((c = getchar()) - '0') < 10);
  return x;
}
int n, m;
const int Maxn = 1e5 + 5;
vector<pair<int, int> > from[Maxn];
priority_queue<pair<long long, int> > q;
long long dist[Maxn];
int pre[Maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < (m); i++) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    from[u].push_back(make_pair(v, w));
    from[v].push_back(make_pair(u, w));
  }
  memset(pre, -1, sizeof(pre));
  for (int i = 0; i < (Maxn); i++) dist[i] = 1e17;
  dist[1] = 0;
  q.push(make_pair(0, 1));
  while (!q.empty()) {
    long long d = -q.top().first;
    int u = q.top().second;
    q.pop();
    if (d != dist[u]) continue;
    for (int i = 0; i < (((int)(from[u]).size())); i++) {
      int to = from[u][i].first;
      long long D = d + (long long)from[u][i].second;
      if (dist[to] > D) {
        dist[to] = D;
        pre[to] = u;
        q.push(make_pair(-D, to));
      }
    }
  }
  if (dist[n] == 1e17) {
    printf("-1\n");
  } else {
    int u = n;
    vector<int> seq;
    while (u != -1) {
      seq.push_back(u);
      u = pre[u];
    }
    for (int i = ((int)(seq).size()) - 1; i >= 0; i--)
      printf("%d%c", seq[i], i ? ' ' : '\n');
  }
  return 0;
}
