#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > g[100001];
int anc[100001];
void printPath(int id) {
  if (id < 0) return;
  printPath(anc[id]);
  printf("%d ", id);
}
int main() {
  memset(anc, -1, sizeof(anc));
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    g[a].push_back(make_pair(b, -w));
    g[b].push_back(make_pair(a, -w));
  }
  priority_queue<tuple<int, int, int> > pq;
  pq.push(make_tuple(0, 1, -2));
  while (!pq.empty()) {
    tuple<int, int, int> t = pq.top();
    int w = get<0>(t);
    int id = get<1>(t);
    int prev = get<2>(t);
    pq.pop();
    if (anc[id] != -1) continue;
    anc[id] = prev;
    if (id == n) {
      printPath(id);
      printf("\n");
      return 0;
    }
    for (pair<int, int> p : g[id])
      if (anc[p.first] == -1) pq.push(make_tuple(w + p.second, p.first, id));
  }
  printf("-1\n");
}
