#include <bits/stdc++.h>
using namespace std;
const long double PI = acos(-1);
const long long INF = LLONG_MAX;
const long long M = 1e5 + 10;
const long long mod = 1e9 + 7;
const long double eps = 1e-9;
void read(long long t[], long long n) {
  for (int i = 1; i <= n; ++i) {
    cin >> t[i];
  }
  return;
}
vector<pair<long long, long long> > G[M];
bool vis[M] = {false};
long long dist[M];
long long parent[M];
vector<long long> path;
void dijkestra(vector<pair<long long, long long> > G[M], long long scr, int V) {
  for (int i = 1; i <= V; i++) dist[i] = INF;
  dist[scr] = 0;
  vis[scr] = true;
  parent[scr] = -1;
  priority_queue<pair<long long, long long>,
                 vector<pair<long long, long long> >,
                 greater<pair<long long, long long> > >
      q;
  q.push({0, scr});
  while (!q.empty()) {
    long long a = q.top().second;
    vis[a] = 1;
    if (dist[a] < q.top().first) {
      q.pop();
      continue;
    }
    q.pop();
    for (auto b : G[a]) {
      if (dist[a] + b.second < dist[b.first] && !vis[b.first]) {
        dist[b.first] = dist[a] + b.second;
        parent[b.first] = a;
        q.push({dist[b.first], b.first});
      }
    }
  }
}
void printPath(long long parent[], long long j) {
  if (parent[j] == -1) return;
  printPath(parent, parent[j]);
  printf("%d ", j);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long V, E, i;
  cin >> V >> E;
  for (int i = 0; i < E; ++i) {
    long long a, b, c;
    cin >> a >> b >> c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }
  dijkestra(G, 1, V);
  if (dist[V] == INF)
    cout << -1;
  else {
    int index = V;
    while (true) {
      path.push_back(index);
      index = parent[index];
      if (index == -1) break;
    }
    reverse(path.begin(), path.end());
    for (auto x : path) cout << x << " ";
    cout << endl;
  }
  return 0;
}
