#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long>>> g(n + 1);
  for (int i = 0; i < m; i++) {
    int v, u, w;
    cin >> v >> u >> w;
    g[v].push_back(make_pair(u, w));
    g[u].push_back(make_pair(v, w));
  }
  vector<long long> d(n + 1, (long long)2e18);
  vector<int> p(n + 1, -1);
  d[1] = 0;
  priority_queue<pair<long long, int>> q;
  q.push({d[1], 1});
  while (!q.empty()) {
    long long v = q.top().second, t = -q.top().first;
    q.pop();
    if (t > d[v]) continue;
    for (auto i : g[v]) {
      long long u = i.first, c = i.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        q.push(make_pair(-d[u], u));
        p[u] = v;
      }
    }
  }
  if (d[n] == (long long)2e18)
    cout << -1;
  else {
    vector<int> r;
    int i = n;
    while (p[i] != -1) {
      r.push_back(i);
      i = p[i];
    }
    r.push_back(1);
    reverse(r.begin(), r.end());
    for (auto i : r) cout << i << " ";
  }
}
