#include <bits/stdc++.h>
template <typename T>
T mymax(T x, T y) {
  return (x > y ? x : y);
}
using namespace std;
const int N = 100005;
const long long inf = 1e18;
vector<int> graph[N], cost[N];
int n, m;
long long dis[N];
struct node {
  int nd, c;
  node(int _nd, int _c) {
    nd = _nd;
    c = _c;
  }
  bool operator<(const node& p) const { return c > p.c; }
};
void dijkstra() {
  priority_queue<node> pq;
  pq.push(node(1, 0));
  for (int i = 0; i < N; i++) dis[i] = inf;
  dis[1] = 0;
  while (!pq.empty()) {
    node top = pq.top();
    pq.pop();
    int u = top.nd;
    for (int i = 0; i < (int)graph[u].size(); i++) {
      int v = graph[u][i];
      if (dis[v] > dis[u] + cost[u][i]) {
        dis[v] = dis[u] + cost[u][i];
        pq.push(node(v, dis[v]));
      }
    }
  }
  queue<int> q;
  q.push(n);
  vector<int> ans;
  ans.push_back(n);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    for (int i = 0; i < graph[u].size(); i++) {
      int v = graph[u][i];
      if (dis[u] - cost[u][i] == dis[v]) {
        ans.push_back(v);
        q.push(v);
        break;
      }
    }
  }
  if (ans.size() == 1) {
    printf("-1\n");
    return;
  }
  reverse((ans).begin(), (ans).end());
  for (int x : ans) {
    printf("%d ", x);
  }
  printf("\n");
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    graph[u].push_back(v);
    graph[v].push_back(u);
    if (cost[u].size() == 0) {
      cost[u].push_back(w);
      cost[v].push_back(w);
    } else {
      int mn = 1e9;
      for (int i = 0; i < graph[u].size() - 1; i++) {
        int adj = graph[u][i];
        if (adj == v) {
          mn = min(mn, cost[u][i]);
        }
      }
      mn = min(mn, w);
      cost[u].push_back(mn);
      cost[v].push_back(mn);
    }
  }
  dijkstra();
  return;
}
int main() {
  solve();
  return 0;
}
