#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
int main() {
  int n, m;
  cin >> n >> m;
  vector<long long> dist(n + 1, INF);
  vector<int> padre(n + 1, -1);
  vector<vector<pair<int, int> > > M(n + 1);
  queue<int> q;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    M[u].push_back(pair<int, int>(c, v));
    M[v].push_back(pair<int, int>(c, u));
  }
  dist[1] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int> >,
                 greater<pair<int, int> > >
      pq;
  pq.push(pair<int, int>(0, 1));
  while (!pq.empty()) {
    pair<int, int> front = pq.top();
    pq.pop();
    int d = front.first, u = front.second;
    if (d > dist[u]) continue;
    for (int j = 0; j < M[u].size(); j++) {
      pair<int, int> v = M[u][j];
      if (dist[u] + v.first < dist[v.second]) {
        dist[v.second] = dist[u] + v.first;
        padre[v.second] = u;
        pq.push(pair<int, int>(dist[v.second], v.second));
      }
    }
  }
  int p = n;
  while (p != -1) {
    q.push(p);
    p = padre[p];
  }
  if (dist[n] == INF) {
    cout << -1 << "\n";
    return 0;
  }
  vector<int> vamoh;
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vamoh.push_back(u);
  }
  for (int i = 0; i < vamoh.size(); i++) {
    cout << vamoh[vamoh.size() - i - 1] << " ";
  }
  cout << "\n";
  return 0;
}
