#include <bits/stdc++.h>
using namespace std;
const long long MAX = 3e5 + 6;
long long n, m, Pre[MAX];
long long d[MAX];
vector<pair<long long, long long> > g[MAX];
void Trace(long long u) {
  stack<long long> st;
  while (u > 0) {
    st.push(u);
    u = Pre[u];
  }
  while (st.size()) cout << st.top() << " ", st.pop();
}
void Dijkstra() {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  for (long long i = 1; i <= n; i++) d[i] = 1e15;
  d[1] = 0;
  pq.push(pair<long long, long long>(0, 1));
  while (!pq.empty()) {
    long long u = pq.top().second, du = pq.top().first;
    pq.pop();
    if (u == n) {
      Trace(n);
      return;
    }
    if (du > d[u]) continue;
    for (pair<long long, long long> t : g[u]) {
      long long v = t.second;
      long long uv = t.first;
      if (du + uv < d[v]) {
        d[v] = d[u] + uv;
        Pre[v] = u;
        pq.push(pair<long long, long long>(d[v], v));
      }
    }
  }
  cout << -1;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long u, v, dd;
    cin >> u >> v >> dd;
    g[u].push_back(pair<long long, long long>(dd, v));
    g[v].push_back(pair<long long, long long>(dd, u));
  }
  Dijkstra();
  return 0;
}
