#include <bits/stdc++.h>
using namespace std;
long long dis[100010], f[100010];
vector<pair<long long, long long> > edge[100010];
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pq;
void print(long long a) {
  if (a > 1) print(f[a]);
  cout << a << ' ';
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n, m, i, a, b, c;
  cin >> n >> m;
  while (m--) {
    cin >> a >> b >> c;
    edge[a].emplace_back(make_pair(b, c));
    edge[b].emplace_back(make_pair(a, c));
  }
  for (i = 2; i <= n; i++) dis[i] = 999999999999999;
  dis[1] = 0;
  pq.push({0, 1});
  while (!pq.empty()) {
    a = pq.top().second;
    pq.pop();
    for (auto sana : edge[a]) {
      b = sana.first;
      c = sana.second;
      if (dis[a] + c < dis[b]) {
        dis[b] = dis[a] + c;
        f[b] = a;
        pq.push({dis[a] + c, b});
      }
    }
  }
  if (dis[n] < 999999999999999)
    print(n);
  else
    cout << "-1\n";
  return 0;
}
