#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
mt19937 gen(time(0));
int n, m;
vector<int> from;
vector<long long> dis;
vector<bool> v;
vector<vector<pair<int, long long>>> edge;
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    pq;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  dis.resize(n, 1e15);
  from.resize(n, 0);
  v.resize(n, 0);
  edge.resize(n);
  int a, b;
  long long c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    --a, --b;
    edge[b].push_back({a, c});
    edge[a].push_back({b, c});
  }
  dis[0] = 0;
  pq.push({dis[0], 0});
  while (!pq.empty()) {
    auto u = pq.top();
    pq.pop();
    if (v[u.second]) continue;
    v[u.second] = 1;
    for (auto i : edge[u.second]) {
      if (dis[i.first] > u.first + i.second) {
        dis[i.first] = u.first + i.second;
        from[i.first] = u.second;
        pq.push({dis[i.first], i.first});
      }
    }
  }
  if (dis[n - 1] == 1e15) {
    cout << -1 << '\n';
    return 0;
  }
  vector<int> ans;
  int x = n - 1;
  while (x != 0) {
    ans.push_back(x);
    x = from[x];
  }
  ans.push_back(x);
  reverse(ans.begin(), ans.end());
  for (auto i : ans) cout << i + 1 << " ";
  cout << '\n';
  return 0;
}
