#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000000;
vector<long long> path;
void Output(long long i) {
  if (i > 1) Output(path[i]);
  cout << i << " ";
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<vector<pair<long long, long long> > > graph(n + 1);
  vector<long long> dis(n + 1, MAX);
  dis[1] = 0;
  path.resize(n + 1);
  priority_queue<pair<long long, long long> > Q;
  int a, b, c;
  for (int i = (0); i < m; i++) {
    cin >> a >> b >> c;
    graph[a].push_back(make_pair(b, c));
    graph[b].push_back(make_pair(a, c));
  }
  Q.push(make_pair(0, 1));
  long long temp, temp2;
  while (!Q.empty()) {
    temp = Q.top().second, temp2 = Q.top().first;
    Q.pop();
    if (temp2 <= dis[temp])
      for (int j = 0; j < graph[temp].size(); j++) {
        long long to = graph[temp][j].first, len = graph[temp][j].second;
        if (dis[temp] + len < dis[to]) {
          dis[to] = dis[temp] + len;
          path[to] = temp;
          Q.push(make_pair(-dis[to], to));
        }
      }
  }
  vector<long long> ans;
  if (dis[n] == MAX)
    cout << -1 << endl;
  else {
    Output(n);
    cout << "" << endl;
  }
}
