#include <bits/stdc++.h>
using namespace std;
void cline() { cout << '\n'; }
template <typename T, typename... V>
void cline(T t, V... v) {
  cout << t;
  if (sizeof...(v)) cout << ' ';
  cline(v...);
}
void cspc() { cout << ' '; }
template <typename T, typename... V>
void cspc(T t, V... v) {
  cout << t;
  if (sizeof...(v)) cout << ' ';
  cspc(v...);
}
const int N = 1e5 + 4;
int source, destination;
long long ans;
int par[N];
long long dist[N];
vector<pair<int, long long>> adj[N];
vector<int> v;
bool visited[N];
void dijkstra() {
  set<pair<long long, int>> se;
  se.insert({0, source});
  while (!se.empty()) {
    int cur = se.begin()->second;
    long long d = se.begin()->first;
    se.erase(*(se.begin()));
    if (visited[cur]) {
      continue;
    }
    if (cur == destination) {
      ans = d;
      break;
    }
    visited[cur] = 1;
    for (auto tmp : adj[cur]) {
      if (visited[tmp.first]) {
        continue;
      }
      if (d + tmp.second < dist[tmp.first]) {
        par[tmp.first] = cur;
        dist[tmp.first] = d + tmp.second;
      }
      se.insert({d + tmp.second, tmp.first});
    }
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int xi, yi;
    long long wi;
    cin >> xi >> yi >> wi;
    adj[xi].push_back({yi, wi});
    adj[yi].push_back({xi, wi});
  }
  ans = -1;
  source = 1, destination = n;
  for (int i = 0; i < n + 5; i++) {
    dist[i] = 1000000000000000000;
  }
  dijkstra();
  if (ans == -1) {
    cspc(-1);
    return 0;
  }
  v.push_back(n);
  int p = par[n];
  while (p != 1) {
    v.push_back(p);
    p = par[p];
  }
  v.push_back(1);
  reverse(v.begin(), v.end());
  for (int x : v) {
    cspc(x);
  }
  return 0;
}
