#include <bits/stdc++.h>
using namespace std;
long long int n, m;
vector<long long int> dist;
vector<vector<pair<long long int, long long int>>> v(100005);
long long int inf = 10e12, path[100005] = {0};
void dij(long long int x) {
  long long int i, j, k, l, strt = 0, edge, d;
  dist[x] = 0;
  priority_queue<long long int> q;
  q.push(x);
  while (q.empty() != true) {
    edge = q.top();
    q.pop();
    d = dist[edge];
    for (i = 0; i < v[edge].size(); i++) {
      if (dist[v[edge][i].first] > d + v[edge][i].second) {
        dist[v[edge][i].first] = d + v[edge][i].second;
        q.push(v[edge][i].first);
        path[v[edge][i].first] = edge;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  dist.assign(n + 1, inf);
  long long int i, j, k, l, a, b, c;
  vector<long long int> vans;
  for (i = 0; i <= n; i++) {
    dist[i] = inf;
  }
  for (i = 0; i < m; i++) {
    cin >> a >> b >> c;
    v[a].push_back(make_pair(b, c));
    v[b].push_back(make_pair(a, c));
  }
  i = 1;
  dij(i);
  if (dist[n] == inf) {
    cout << -1;
  } else {
    i = n;
    while (i != 0) {
      vans.push_back(i);
      i = path[i];
    }
    for (i = vans.size() - 1; i >= 0; i--) {
      cout << vans[i] << " ";
    }
  }
}
