#include <bits/stdc++.h>
using namespace std;
using iil = pair<long long, long long>;
int n;
vector<vector<iil> > adj;
vector<int> p;
vector<long long> d;
void dijkstra(int s = 0) {
  d.resize(n, LLONG_MAX);
  d[s] = 0;
  p.resize(n);
  priority_queue<iil, vector<iil>, greater<iil> > q;
  q.push({d[s], s});
  while (!q.empty()) {
    int v = q.top().second;
    long long d_v = q.top().first;
    q.pop();
    if (d_v != d[v]) continue;
    for (auto& edge : adj[v]) {
      int to = edge.first;
      long long len = edge.second;
      if (d[v] + len < d[to]) {
        d[to] = d[v] + len;
        p[to] = v;
        q.push({d[to], to});
      }
    }
  }
}
int main() {
  int m;
  cin >> n >> m;
  adj.resize(n);
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    long long w;
    cin >> w;
    a--;
    b--;
    adj[a].push_back({b, w});
    adj[b].push_back({a, w});
  }
  dijkstra();
  if (d[n - 1] == LLONG_MAX) {
    cout << -1;
  } else {
    int v = n - 1;
    vector<int> road;
    while (v) {
      road.push_back(v);
      v = p[v];
    }
    cout << 1 << " ";
    for (int i = (int)road.size() - 1; i >= 0; i--) cout << road[i] + 1 << " ";
  }
  return 0;
}
