#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> G[int(10e5) + 2];
unsigned long long int W[(int)(10e5)];
int back[(int)(10e5)];
int n, m;
void findPath() {
  priority_queue<pair<unsigned long long int, int>,
                 vector<pair<unsigned long long int, int>>,
                 greater<pair<unsigned long long int, int>>>
      pq;
  pq.push(make_pair(0, 1));
  W[1] = 0;
  while (!pq.empty()) {
    auto u = pq.top().second;
    pq.pop();
    for (auto i : G[u]) {
      int v = i.first;
      unsigned long long int w = i.second;
      if (W[v] > W[u] + w) {
        W[v] = W[u] + w;
        back[v] = u;
        pq.push(make_pair(W[v], v));
      }
    }
  }
}
void printPath(int u) {
  if (back[u] > 0) printPath(back[u]);
  cout << u << " ";
}
int main() {
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) W[i] = 10e12, back[i] = -1;
  for (int i = 0; i < m; i++) {
    int uu, vv, ww;
    cin >> uu >> vv >> ww;
    G[uu].push_back(make_pair(vv, ww));
    G[vv].push_back(make_pair(uu, ww));
  }
  findPath();
  if (back[n] > 0)
    printPath(n);
  else
    cout << "-1\n";
  return 0;
}
