#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[100005];
priority_queue<pair<long long, long long> > pq;
stack<long long> q;
long long dist[100005], a, b, w, n, m, bef[100005];
bool processed[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> w;
    adj[a].push_back(make_pair(w, b));
    adj[b].push_back(make_pair(w, a));
  }
  for (int i = 1; i <= n; i++) dist[i] = 1e18;
  memset(processed, 0, sizeof processed);
  memset(bef, -1, sizeof bef);
  dist[1] = 0;
  bef[1] = 0;
  pq.push(make_pair(0, 1));
  while (!pq.empty()) {
    long long a = pq.top().second;
    pq.pop();
    if (processed[a]) {
      continue;
    }
    processed[a] = true;
    for (auto u : adj[a]) {
      if (dist[u.second] > dist[a] + u.first) {
        dist[u.second] = dist[a] + u.first;
        bef[u.second] = a;
        pq.push(make_pair(-dist[u.second], u.second));
      }
    }
  }
  if (bef[n] == -1) {
    cout << "-1" << '\n';
    return 0;
  }
  long long x = n;
  bef[1] = 0;
  while (x != 0) {
    q.push(x);
    if (bef[x] == -1) {
      cout << "-1" << '\n';
      return 0;
    }
    x = bef[x];
  }
  long long qs = q.size();
  cout << q.top();
  q.pop();
  for (int i = 2; i <= qs; i++) {
    cout << " " << q.top();
    q.pop();
  }
  cout << '\n';
}
