#include <bits/stdc++.h>
using namespace std;
map<int, vector<pair<int, int>>> adj;
vector<long long> dis(100010, 1e16), parent(100010);
priority_queue<int> q;
int v, e, from, to, w;
void print_path(int node) {
  if (node != 1) print_path(parent[node]);
  cout << node << " ";
}
void dijkstra(int node) {
  q.push(node), dis[node] = 0;
  while (!q.empty()) {
    int cur = q.top();
    q.pop();
    for (int i = 0; i < adj[cur].size(); i++) {
      int child = adj[cur][i].first, W = adj[cur][i].second;
      if (dis[cur] + W < dis[child]) {
        dis[child] = dis[cur] + W;
        q.push(child);
        parent[child] = cur;
      }
    }
  }
}
int main() {
  cin >> v >> e;
  for (int i = 1; i <= e; i++) {
    cin >> from >> to >> w;
    adj[from].push_back({to, w});
    adj[to].push_back({from, w});
  }
  dijkstra(1);
  if (dis[v] == 1e16)
    cout << "-1";
  else
    print_path(v);
  return 0;
}
