#include <bits/stdc++.h>
using namespace std;
void trace(vector<long long> &prev, long long node) {
  if (prev[node] == -1) {
    cout << node + 1 << " ";
  } else {
    trace(prev, prev[node]);
    cout << node + 1 << " ";
  }
}
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> g[n];
  vector<long long> cost[n];
  for (int i = 0; i < m; i++) {
    long long x, y, w;
    cin >> x >> y >> w;
    x--, y--;
    g[x].push_back(y);
    g[y].push_back(x);
    cost[x].push_back(w);
    cost[y].push_back(w);
  }
  vector<long long> dis(n, LONG_LONG_MAX);
  priority_queue<pair<long long, long long>, vector<pair<long long, long long>>,
                 greater<pair<long long, long long>>>
      myq;
  dis[0] = 0;
  vector<long long> prev(n);
  myq.push({0, 0});
  prev[0] = -1;
  while (!myq.empty()) {
    pair<long long, long long> temp = myq.top();
    myq.pop();
    long long x = temp.second;
    for (int i = 0; i < g[x].size(); i++) {
      long long y = g[x][i];
      long long tw = cost[x][i];
      if (dis[x] + tw < dis[y]) {
        dis[y] = dis[x] + tw;
        myq.push({dis[y], y});
        prev[y] = x;
      }
    }
  }
  if (dis[n - 1] == LONG_LONG_MAX) {
    cout << -1 << endl;
  } else {
    trace(prev, n - 1);
  }
  return 0;
}
