#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> ar[100007 + 5];
int dis[100007 + 5];
int visit[100007 + 5];
int par[100007 + 5];
bool bfs(int node, int t) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, node});
  dis[node] = 0;
  par[node] = -1;
  while (!q.empty()) {
    int curn = q.top().second;
    int curd = q.top().first;
    q.pop();
    if (curn == t) return true;
    visit[curn] = 1;
    for (pair<int, int> p : ar[curn]) {
      if (curd + p.second < dis[p.first] && visit[p.first] == 0) {
        dis[p.first] = curd + p.second;
        q.push({dis[p.first], p.first});
        par[p.first] = curn;
      }
    }
  }
  return false;
}
int main() {
  int n, m, w, a, b;
  cin >> n >> m;
  for (int i = 1; i <= 100007; i++) {
    dis[i] = INT_MAX;
  }
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> w;
    ar[a].push_back({b, w});
    ar[b].push_back({a, w});
  }
  if (bfs(1, n)) {
    vector<int> path;
    for (b = n; b != -1; b = par[b]) {
      path.push_back(b);
    }
    for (int i = path.size() - 1; i >= 0; i--) cout << path[i] << " ";
    cout << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;
}
