#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > adj[100005];
int m, n, a, b, w;
long long D[100005];
int Tr[100005];
void dijk(int x) {
  Tr[x] = 0;
  D[x] = 0;
  set<pair<long long, int> > s;
  s.insert({0, x});
  while (s.size() > 0) {
    long long d = (*s.begin()).first;
    int u = (*s.begin()).second;
    s.erase(s.begin());
    for (int i = 0; i < adj[u].size(); ++i) {
      int v = adj[u][i].second;
      int w = adj[u][i].first;
      if (D[v] > D[u] + w) {
        D[v] = D[u] + w;
        s.insert({D[v], v});
        Tr[v] = u;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> a >> b >> w;
    adj[a].push_back({w, b});
    adj[b].push_back({w, a});
  }
  memset(D, 63, sizeof(D));
  dijk(1);
  if (D[n] > 1e12) {
    cout << -1;
    return 0;
  }
  vector<int> p;
  int t = n;
  while (t > 0) {
    p.push_back(t);
    t = Tr[t];
  }
  reverse(p.begin(), p.end());
  for (int i = 0; i < p.size(); ++i) cout << p[i] << " ";
  return 0;
}
