#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long>> adj[111111];
bool visited[111111];
priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
               greater<pair<long long, long long>>>
    pq;
long long dist[111111];
long long par[111111];
void dijk(int src) {
  fill(dist, dist + 111111, 1e18);
  fill(par, par + 111111, -1);
  pq.push(pair<long long, long long>(0, src));
  dist[1] = 0;
  while (not pq.empty()) {
    long long u = pq.top().second;
    pq.pop();
    if (visited[u] == true) {
      continue;
    }
    visited[u] = true;
    for (pair<long long, long long> edge : adj[u]) {
      long long v = edge.first;
      long long w = edge.second;
      if (dist[v] > dist[u] + w) {
        dist[v] = dist[u] + w;
        pq.push(pair<long long, long long>(dist[v], v));
        par[v] = u;
      }
    }
  }
}
void display(int dest) {
  if (par[dest] == -1) {
    return;
  }
  display(par[dest]);
  cout << dest << " ";
}
int main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long a, b, w;
    cin >> a >> b >> w;
    adj[a].emplace_back(pair<long long, long long>(b, w));
    adj[b].emplace_back(pair<long long, long long>(a, w));
  }
  dijk(1);
  if (dist[n] == 1e18) {
    cout << -1 << endl;
  } else
    cout << 1 << " ";
  display(n);
  cout << endl;
  return 0;
}
