#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e+18;
vector<pair<long long, long long> > g[100001];
long long p[100001], d[100001];
vector<long long> path;
set<pair<long long, long long> > S;
int main() {
  long long n, m, a, b, w;
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) {
    d[i] = INF;
  }
  while (m--) {
    cin >> a >> b >> w;
    if (a != b) {
      g[a].push_back(make_pair(b, w));
      g[b].push_back(make_pair(a, w));
    }
  }
  long long s = 1, f = n;
  d[s] = 0;
  S.insert(make_pair(d[s], s));
  while (!S.empty()) {
    long long v = S.begin()->second;
    S.erase(S.begin());
    for (long long i = 0; i < g[v].size(); i++) {
      long long to = g[v][i].first, len = g[v][i].second;
      if (d[v] + len < d[to]) {
        S.erase(make_pair(d[to], to));
        d[to] = d[v] + len;
        S.insert(make_pair(d[to], to));
        p[to] = v;
      }
    }
  }
  if (d[f] == INF) {
    cout << -1;
    return 0;
  }
  for (long long i = f; i != s; i = p[i]) {
    path.push_back(i);
  }
  cout << s << " ";
  for (long long i = path.size() - 1; i >= 0; i--) {
    cout << path[i] << " ";
  }
  return 0;
}
