#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const long long INF = 1LL << 60;
vector<pair<int, int> > g[MAXN];
int p[MAXN];
long long d[MAXN];
void dijkstra(int s, int t, int n) {
  for (int i = 0; i <= n; ++i) {
    p[i] = -1;
    d[i] = INF;
  }
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      q;
  q.push(pair<long long, int>(0, s));
  d[s] = 0;
  p[s] = -2;
  while (q.size() > 0) {
    int cur = q.top().second;
    long long dist = q.top().first;
    q.pop();
    if (cur == t) return;
    if (dist > d[cur]) continue;
    for (int i = 0; i < g[cur].size(); ++i) {
      int next = g[cur][i].first;
      int w_extra = g[cur][i].second;
      if (dist + w_extra < d[next]) {
        p[next] = cur;
        d[next] = dist + w_extra;
        q.push(pair<long long, int>(d[next], next));
      }
    }
  }
  assert(p[t] == -1);
}
int main() {
  ;
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i <= n; ++i) g[i].clear();
    for (int i = 0; i < m; ++i) {
      int a, b, w;
      cin >> a >> b >> w;
      g[a].push_back(pair<int, int>(b, w));
      g[b].push_back(pair<int, int>(a, w));
    }
    dijkstra(1, n, n);
    if (p[n] != -1) {
      vector<int> path;
      int cur = n;
      while (cur != -2) {
        path.push_back(cur);
        cur = p[cur];
      }
      for (int i = path.size() - 1; i >= 0; --i) {
        printf("%d", path[i]);
        if (i != 0) printf(" ");
      }
      printf("\n");
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
