#include <bits/stdc++.h>
using namespace std;
bool comp(pair<long long, long long> p1, pair<long long, long long> p2) {
  return (p1.second != p2.second ? p1.second > p2.second : p1.first > p2.first);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long>>> G(n);
  while (m--) {
    long long a, b, w;
    cin >> a >> b >> w;
    G[--a].emplace_back(--b, w);
    G[b].emplace_back(a, w);
  }
  priority_queue<
      pair<long long, long long>, vector<pair<long long, long long>>,
      function<bool(pair<long long, long long>, pair<long long, long long>)>>
      pq(comp);
  vector<long long> D(n, LLONG_MAX);
  D[0] = 0;
  vector<int> neighbor(n, -1);
  pq.emplace(0, 0);
  while (!pq.empty()) {
    pair<long long, long long> u = pq.top();
    pq.pop();
    for (pair<long long, long long> v : G[u.first])
      if (D[v.first] > u.second + v.second) {
        neighbor[v.first] = u.first;
        D[v.first] = u.second + v.second;
        pq.emplace(v.first, D[v.first]);
      }
  }
  if (D[n - 1] == LLONG_MAX) {
    cout << -1;
    return 0;
  }
  stack<int> output;
  int vertex = n - 1;
  while (vertex != -1) {
    output.push(vertex + 1);
    vertex = neighbor[vertex];
  }
  while (!output.empty()) {
    cout << output.top() << ' ';
    output.pop();
  }
  return 0;
}
