#include <bits/stdc++.h>
using namespace std;
struct node {
  int index;
  int value;
  bool operator<(node n) const { return value > n.value; }
};
int main() {
  ios_base::sync_with_stdio(false);
  node nd, temp;
  int x, y, n, m, v;
  cin >> n >> m;
  vector<int> visited(n, 0), weight(n, INT_MAX), rez;
  vector<vector<node> > adj(n);
  map<int, int> parent;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> v;
    nd.value = v;
    nd.index = y - 1;
    adj[x - 1].push_back(nd);
    nd.index = x - 1;
    adj[y - 1].push_back(nd);
  }
  weight[0] = 0;
  priority_queue<node> pq;
  nd.index = nd.value = 0;
  pq.push(nd);
  while (!pq.empty()) {
    nd = pq.top();
    pq.pop();
    if (visited[nd.index]) continue;
    visited[nd.index] = 1;
    for (int i = 0; i < adj[nd.index].size(); i++) {
      temp = adj[nd.index][i];
      if (!visited[temp.index] && weight[temp.index] > nd.value + temp.value) {
        weight[temp.index] = nd.value + temp.value;
        temp.value = weight[temp.index];
        parent[temp.index] = nd.index;
        pq.push(temp);
      }
    }
  }
  if (!visited[n - 1]) {
    cout << -1;
    return 0;
  }
  int index = n - 1;
  while (index != 0) {
    rez.push_back(parent[index]);
    index = parent[index];
  }
  for (int i = rez.size() - 1; i >= 0; i--) cout << rez[i] + 1 << " ";
  cout << n;
  return 0;
}
