#include <bits/stdc++.h>
using namespace std;
long long INF = 1e18;
vector<pair<long long, long long>> ad[100001];
vector<long long> d(100001), p(100001);
void dijkstra(long long s, long long t) {
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      q;
  q.push({0, s});
  d.assign(d.size(), INF);
  d[s] = 0;
  while (!q.empty()) {
    long long v = q.top().second;
    long long dv = q.top().first;
    q.pop();
    if (dv != d[v]) continue;
    if (v == t) {
      break;
    }
    for (auto k : ad[v]) {
      long long w = k.second;
      long long u = k.first;
      if (d[v] + w < d[u]) {
        d[u] = d[v] + w;
        p[u] = v;
        q.push({d[u], u});
      }
    }
  }
  if (d[t] != INF) {
    vector<long long> path;
    while (t != s) {
      path.push_back(t);
      t = p[t];
    }
    path.push_back(s);
    reverse(path.begin(), path.end());
    for (long long I = 0; I < path.size(); I++) cout << path[I] << " ";
    cout << "\n";
    ;
    return;
  }
  cout << "-1\n";
}
void solve() {
  long long n, m;
  cin >> n >> m;
  while (m--) {
    long long u, v, w;
    cin >> u >> v >> w;
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
  }
  dijkstra(1, n);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
