#include <bits/stdc++.h>
using namespace std;
int mod = 998244353;
int inf = (1e9) + 7;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<pair<int, int>> grafo[200005];
  for (int i = 0; i < m; ++i) {
    int a, b, w;
    scanf("%d %d %d", &a, &b, &w);
    grafo[a].push_back(make_pair(w, b));
    grafo[b].push_back(make_pair(w, a));
  }
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      priority_queue;
  priority_queue.push(make_pair(0, 1));
  vector<int> pai(200005, 0);
  vector<long long> best_path(200005, 1ll * inf * inf);
  best_path[1] = 0;
  while (priority_queue.size()) {
    auto u = priority_queue.top();
    priority_queue.pop();
    for (auto w : grafo[u.second]) {
      if (best_path[w.second] > u.first + w.first) {
        best_path[w.second] = u.first + w.first;
        pai[w.second] = u.second;
        priority_queue.push(make_pair(w.first + u.first, w.second));
      }
    }
  }
  if (best_path[n] == 1ll * inf * inf) {
    printf("-1\n");
    return 0;
  }
  vector<int> ans;
  int k = n;
  while (1) {
    ans.push_back(k);
    if (k == 1) break;
    k = pai[k];
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < ans.size(); ++i) {
    printf("%d ", ans[i]);
  }
  printf("\n");
}
