#include <bits/stdc++.h>
using namespace ::std;
const int INF = int(1e9);
const long long INF64 = (long long)(1e18);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long> > > g(n + 1);
  for (long long i = 0; i < m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    g[a].push_back(make_pair(b, w));
    g[b].push_back(make_pair(a, w));
  }
  long long s = 1, f = n;
  vector<long long> dist(n + 1, INF64);
  dist[s] = 0;
  set<pair<long long, long long> > unused;
  unused.insert(make_pair(0, s));
  vector<long long> prev(n + 1);
  prev[s] = -1;
  while (!unused.empty()) {
    long long next = unused.begin()->second;
    unused.erase(unused.begin());
    for (auto edge : g[next]) {
      long long to = edge.first;
      long long wt = edge.second;
      if (dist[next] + wt < dist[to]) {
        unused.erase(make_pair(dist[to], to));
        dist[to] = dist[next] + wt;
        unused.insert(make_pair(dist[to], to));
        prev[to] = next;
      }
    }
  }
  if (dist[f] == INF64) return cout << -1, 0;
  long long curr = f;
  vector<long long> ans;
  while (curr != -1) {
    ans.push_back(curr);
    curr = prev[curr];
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << ' ';
  }
  return 0;
}
