#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long INF = 1e12;
const long long mod = 1e9 + 7;
vector<pair<long long, long long> > adj[N];
long long n, m;
vector<long long> dist(N, INF), par(N, -1);
vector<bool> visited(N, false);
void bfs() {
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      pq;
  long long src = 1;
  dist[src] = 0;
  pq.push(make_pair(0, src));
  while (!pq.empty()) {
    pair<long long, long long> node = pq.top();
    pq.pop();
    visited[node.second] = true;
    for (long long i = 0; i < adj[node.second].size(); i++) {
      pair<long long, long long> ele = adj[node.second][i];
      if (!visited[ele.first] &&
          dist[ele.first] > dist[node.second] + ele.second) {
        dist[ele.first] = dist[node.second] + ele.second;
        pq.push(make_pair(dist[ele.first], ele.first));
        par[ele.first] = node.second;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  long long u, v, w;
  for (long long i = 0; i < m; i++) {
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  bfs();
  if (dist[n] >= INF) return cout << "-1", 0;
  vector<long long> path;
  while (par[n]) {
    path.push_back(n);
    if (n == 1) break;
    n = par[n];
  }
  reverse(path.begin(), path.end());
  for (long long i = 0; i < path.size(); i++) cout << path[i] << " ";
}
