#include <bits/stdc++.h>
using namespace std;
struct data {
  int u;
  long long w;
  data() {}
  data(int a, long long b) {
    u = a;
    w = b;
  }
  bool operator<(const data& p) const { return p.w < w; }
};
int par[100005];
long long dist[100005];
vector<pair<int, int> > adj[100005];
long long dijkstra(int end) {
  for (int i = 0; i <= end; i++) dist[i] = 100000000000;
  memset(par, -1, sizeof(par));
  dist[1] = 0;
  priority_queue<data> q;
  q.push(data(1, 0));
  while (!q.empty()) {
    int u = q.top().u;
    long long w = q.top().w;
    q.pop();
    if (u == end) return (dist[end]);
    if (dist[u] < w) continue;
    for (int i = 0; i < (int)adj[u].size(); i++) {
      int v = adj[u][i].first;
      long long wsec = adj[u][i].second;
      if (dist[v] <= w + wsec) continue;
      par[v] = u;
      dist[v] = w + wsec;
      q.push(data(v, dist[v]));
    }
  }
  return 100000000000;
}
int main() {
  int i, j, k, v, e, u1, u2, w;
  while (scanf("%d %d", &v, &e) == 2) {
    for (i = 0; i <= v; i++) adj[i].clear();
    for (i = 0; i < e; i++) {
      scanf("%d %d %d", &u1, &u2, &w);
      adj[u1].push_back(make_pair(u2, w));
      adj[u2].push_back(make_pair(u1, w));
    }
    long long res = dijkstra(v);
    if (res == 100000000000)
      printf("-1\n");
    else {
      int k = v;
      vector<int> ans;
      ans.clear();
      while (k != -1) {
        ans.push_back(k);
        k = par[k];
      }
      reverse(ans.begin(), ans.end());
      for (i = 0; i < ans.size(); i++) {
        if (i == ans.size() - 1)
          printf("%d\n", ans[i]);
        else
          printf("%d ", ans[i]);
      }
    }
  }
  return 0;
}
