#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long vis[N], dist[N], parent[N];
vector<pair<long long, long long>> g[N];
long long n, m;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 2; i <= n; i++) dist[i] = 1e18;
  for (long long i = 1; i <= m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  set<pair<long long, long long>> s;
  s.insert({0, 1});
  while (!s.empty()) {
    auto temp = *(s.begin());
    s.erase(temp);
    vis[temp.second] = 1;
    for (auto it : g[temp.second]) {
      if (dist[it.first] > dist[temp.second] + it.second) {
        s.erase({dist[it.first], it.first});
        dist[it.first] = dist[temp.second] + it.second;
        s.insert({dist[it.first], it.first});
        parent[it.first] = temp.second;
      }
    }
  }
  if (!vis[n]) {
    cout << "-1"
         << "\n";
    return 0;
  }
  long long v = n;
  vector<long long> ans;
  while (parent[v]) {
    ans.push_back(v);
    v = parent[v];
  }
  ans.push_back(1);
  reverse(ans.begin(), ans.end());
  for (auto it : ans) {
    cout << it << " ";
  }
  return 0;
}
