#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6;
long long n, m;
long long arrival[N], departure[N], vis[N], parent[N];
vector<pair<long long, long long>> g[N];
void dijsktra(long long src, long long dest) {
  for (long long i = 1; i <= n; i++) {
    arrival[i] = 1e14;
    departure[i] = 1e14;
    vis[i] = 0;
  }
  arrival[src] = 0;
  set<pair<long long, long long>> s;
  s.insert({0, src});
  while (!s.empty()) {
    auto x = *(s.begin());
    s.erase(x);
    vis[x.second] = 1;
    departure[x.second] = arrival[x.second];
    for (auto it : g[x.second]) {
      if (arrival[it.first] > departure[x.second] + it.second) {
        s.erase({arrival[it.first], it.first});
        arrival[it.first] = departure[x.second] + it.second;
        s.insert({arrival[it.first], it.first});
        parent[it.first] = x.second;
      }
    }
  }
  if (!vis[dest]) {
    cout << "-1";
    return;
  }
  long long b = dest;
  vector<long long> v;
  while (parent[b]) {
    v.push_back(b);
    b = parent[b];
  }
  v.push_back(src);
  reverse(v.begin(), v.end());
  for (auto it : v) cout << it << " ";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  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});
  }
  dijsktra(1, n);
  return 0;
}
