#include <bits/stdc++.h>
using namespace std;
vector<pair<int, long long int> > vg[100000];
vector<int> myFunction(int start, int target) {
  long long int cost[target + 1];
  for (int i = 1; i <= target; i++) cost[i] = 10000000000000;
  cost[start] = 0;
  priority_queue<pair<long long int, int>, vector<pair<long long int, int> >,
                 greater<pair<long long int, int> > >
      pq;
  pq.push({0, 1});
  long long int path[target + 1];
  path[start] = 0;
  while (!pq.empty()) {
    auto it = pq.top();
    pq.pop();
    long long int c = it.first;
    int root = it.second;
    for (int i = 0; i < vg[root].size(); i++) {
      int y = vg[root][i].first;
      long long int cc = vg[root][i].second;
      if (cost[y] > cc + cost[root]) {
        cost[y] = cc + cost[root];
        pq.push({cost[y], y});
        path[y] = root;
      }
    }
  }
  vector<int> ans;
  if (cost[target] == 10000000000000) {
    ans.push_back(-1);
    return ans;
  }
  while (path[target] != 0) {
    ans.push_back(target);
    target = path[target];
  }
  ans.push_back(1);
  return ans;
}
int main() {
  int n, m, u, v;
  long long int cost;
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    cin >> u >> v >> cost;
    vg[u].push_back({v, cost});
    vg[v].push_back({u, cost});
  }
  vector<int> ans = myFunction(1, n);
  for (long long int i = ans.size() - 1; i > -1; i--) cout << ans[i] << " ";
  return 0;
}
