#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, u, v, w, d[100001], b[100001], prev[100001], x, y, i,
      j = 0, c[100001];
  std::vector<int> p[100001];
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  scanf("%lld %lld", &n, &m);
  for (i = 1; i <= m; i++) {
    scanf("%lld %lld %lld", &u, &v, &w);
    if (u != v) {
      p[u].push_back(v);
      p[u].push_back(w);
      p[v].push_back(u);
      p[v].push_back(w);
    }
  }
  for (i = 1; i <= 100000; i++) {
    d[i] = 1000000000000;
  }
  d[1] = 0;
  for (i = 1; i <= 100000; i++) {
    b[i] = 0;
    prev[i] = 0;
  }
  pq.push(make_pair(0, 1));
  while (pq.size() != 0) {
    pair<int, int> z = pq.top();
    pq.pop();
    x = z.second;
    y = z.first;
    if (x == n) {
      break;
    }
    if (b[x] == 1) {
      continue;
    }
    b[x] = 1;
    for (auto k = p[x].begin(); k != p[x].end(); k++) {
      if (b[*k] != 1 && (*(k + 1) + d[x]) < d[*k]) {
        d[*k] = *(k + 1) + d[x];
        pq.push(make_pair(d[*k], *k));
        prev[*k] = x;
      }
      k++;
    }
  }
  i = n;
  j = 1;
  c[1] = n;
  if (prev[i] == 0) {
    c[1] = -1;
  }
  while (prev[i] != 0) {
    j++;
    c[j] = prev[i];
    i = prev[i];
  }
  for (i = j; i >= 1; i--) {
    printf("%lld ", c[i]);
  }
  return 0;
}
