#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e11;
const int M = 1e5 + 5;
vector<pair<long long, int> > adj[M];
vector<int> ans;
long long d[M], vis[M], pre[M];
int n, m;
void Dijkstra() {
  for (int i = 1; i <= n; ++i) d[i] = INF;
  d[1] = 0;
  priority_queue<pair<long long, int> > q;
  q.push(make_pair(d[1], 1));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = 0; i < adj[u].size(); ++i) {
      int v = adj[u][i].second;
      long long dist = adj[u][i].first + d[u];
      if (d[v] > dist) {
        d[v] = dist;
        pre[v] = u;
        q.push(make_pair(-d[v], v));
      }
    }
  }
}
int main() {
  int u, v, w;
  cin >> n >> m;
  while (m--) {
    cin >> u >> v >> w;
    adj[u].push_back(make_pair(w, v));
    adj[v].push_back(make_pair(w, u));
  }
  Dijkstra();
  if (d[n] == INF)
    cout << "-1" << endl;
  else {
    for (int i = n; pre[i]; i = pre[i]) ans.push_back(pre[i]);
    for (int i = ans.size() - 1; i >= 0; --i) cout << ans[i] << ' ';
    cout << n << ' ';
    cout << endl;
  }
}
