#include <bits/stdc++.h>
using namespace std;
bool mark[100007] = {0};
vector<pair<long long int, long long int> > adj[100007];
vector<long long int> ans;
long long int d[100007];
long long int par[100007];
void dijkstra(long long int v) {
  d[v] = 0;
  long long int u;
  priority_queue<pair<long long int, long long int>,
                 vector<pair<long long int, long long int> >,
                 greater<pair<long long int, long long int> > >
      pq;
  pq.push(make_pair(d[v], v));
  while (!pq.empty()) {
    u = pq.top().second;
    pq.pop();
    if (mark[u]) continue;
    mark[u] = true;
    for (auto p : adj[u])
      if (d[p.first] > d[u] + p.second) {
        par[p.first] = u;
        d[p.first] = d[u] + p.second;
        pq.push({d[p.first], p.first});
      }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n, m;
  cin >> n >> m;
  for (long long int i = 1; i <= m; i++) {
    long long int a, b, w;
    cin >> a >> b >> w;
    adj[a].push_back(make_pair(b, w));
    adj[b].push_back(make_pair(a, w));
  }
  for (long long int i = 1; i <= n; i++) {
    d[i] = 100000000000000;
    par[i] = -1;
  }
  dijkstra(1);
  if (d[n] == 100000000000000)
    cout << "-1" << endl;
  else {
    long long int x = n;
    while (x != 1) {
      ans.push_back(x);
      x = par[x];
    }
    ans.push_back(1);
    reverse(ans.begin(), ans.end());
    for (auto it : ans) cout << it << " ";
    cout << endl;
  }
  return 0;
}
