#include <bits/stdc++.h>
using namespace std;
const long long INF = ~(1LL << 63);
const int MAXN = 200005;
int n, m;
int st, fi;
int p[MAXN];
long long d[MAXN];
vector<pair<int, long long> > g[MAXN];
bool cmp(int a, int b) { return d[a] != d[b] ? d[a] < d[b] : a < b; }
long long dij() {
  fill(d, d + n, INF);
  set<int, bool (*)(int, int)> q(cmp);
  d[st] = 0;
  q.insert(st);
  while (!q.empty()) {
    int v = *q.begin();
    q.erase(q.begin());
    for (auto i : g[v]) {
      int len = i.second;
      int to = i.first;
      if (d[to] > d[v] + len) {
        q.erase(to);
        d[to] = d[v] + len;
        p[to] = v;
        q.insert(to);
      }
    }
  }
  return d[fi];
}
int main() {
  scanf("%d%d", &n, &m);
  st = 0, fi = n - 1;
  for (int i = 0; i < m; ++i) {
    int a, b, len;
    scanf("%d%d%d", &a, &b, &len);
    --a, --b;
    g[a].push_back(make_pair(b, len));
    g[b].push_back(make_pair(a, len));
  }
  long long len = dij();
  if (len == INF) {
    printf("-1");
  } else {
    vector<int> path;
    for (int i = fi; i != st; i = p[i]) {
      path.push_back(i);
    }
    path.push_back(st);
    reverse(path.begin(), path.end());
    for (auto i : path) {
      printf("%d ", i + 1);
    }
  }
}
