#include <bits/stdc++.h>
using namespace std;
unsigned long long parent[1000005];
unsigned long long dist[1000005];
void printPath(unsigned long long node) {
  if (parent[node] == node) {
    printf("%llu ", node);
    return;
  }
  printPath(parent[node]);
  printf("%llu ", node);
}
int main() {
  unsigned long long m, n, p, q, w;
  scanf("%llu %llu", &n, &m);
  vector<pair<unsigned long long, unsigned long long> > graph[n + 1];
  for (unsigned long long i = 1; i <= n; i++) dist[i] = 1e18;
  while (m--) {
    scanf("%llu %llu %llu", &p, &q, &w);
    graph[p].push_back(pair<unsigned long long, unsigned long long>(q, w));
    graph[q].push_back(pair<unsigned long long, unsigned long long>(p, w));
  }
  priority_queue<pair<unsigned long long, unsigned long long>,
                 vector<pair<unsigned long long, unsigned long long> >,
                 greater<pair<unsigned long long, unsigned long long> > >
      pq;
  dist[1] = 0;
  parent[1] = 1;
  parent[n] = -1;
  pq.push(pair<unsigned long long, unsigned long long>(0, 1));
  while (!pq.empty()) {
    unsigned long long u = pq.top().second;
    unsigned long long c = pq.top().first;
    pq.pop();
    if (dist[u] < c) continue;
    for (unsigned long long i = 0; i < graph[u].size(); i++) {
      unsigned long long node = graph[u][i].first;
      unsigned long long cost = graph[u][i].second;
      if (dist[u] + cost < dist[node]) {
        parent[node] = u;
        dist[node] = dist[u] + cost;
        pq.push(pair<unsigned long long, unsigned long long>(dist[node], node));
      }
    }
  }
  if (parent[n] == -1)
    printf("-1");
  else
    printPath(n);
}
