#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > adj[100005];
long long nV, nE;
void print(long long node, long long parent[]) {
  if (node != 1) {
    print(parent[node], parent);
  }
  cout << node << " ";
}
void dijkstra(long long s) {
  set<pair<long long, long long> > pq;
  long long dist[100005 + 1];
  long long parent[100005 + 1];
  for (int i = 0; i < nV + 1; i++) {
    dist[i] = 10000000000000LL;
    parent[i] = 0;
  }
  pq.insert(pair<long long, long long>(0, s));
  dist[s] = 0;
  while (!pq.empty()) {
    long long u = (*pq.begin()).second;
    long long uw = (*pq.begin()).first;
    pq.erase(pq.begin());
    for (int i = 0; i < adj[u].size(); i++) {
      long long v = adj[u][i].first;
      long long w = adj[u][i].second;
      long long directDistance = dist[v];
      long long calculatedDistance = (long long)((long long)uw + (long long)w);
      if (parent[v] == 0 || directDistance > calculatedDistance) {
        dist[v] = calculatedDistance;
        parent[v] = u;
        pq.insert(pair<long long, long long>(uw + w, v));
      }
    }
  }
  if (parent[nV] != 0) {
    print(nV, parent);
  } else {
    cout << -1 << endl;
  }
}
int main() {
  long long u, v, w;
  cin >> nV >> nE;
  for (int i = 0; i < nE; i++) {
    cin >> u >> v >> w;
    adj[u].push_back(pair<long long, long long>(v, w));
    adj[v].push_back(pair<long long, long long>(u, w));
  }
  long long root = 1;
  dijkstra(root);
}
