#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
const long long INF = 1e18;
long long power(long long x, long long y) {
  long long t = 1;
  while (y > 0) {
    if (y % 2)
      y -= 1, t = t * x % mod;
    else
      y /= 2, x = x * x % mod;
  }
  return t;
}
vector<vector<pair<int, long long>>> G;
set<pair<long long, int>> s;
vector<long long> dis;
vector<int> par;
void dijkstra(int x) {
  int n = G.size();
  dis.resize(n);
  par.resize(n);
  for (int i = 0; i < n; ++i) {
    dis[i] = INF;
    par[i] = -1;
  }
  dis[x] = 0;
  for (int i = 0; i < n; ++i) s.insert(make_pair(dis[i], i));
  while (!s.empty()) {
    auto curr = s.begin();
    int v = curr->second, val = curr->first;
    s.erase(s.begin());
    for (auto u : G[v]) {
      if (dis[u.first] > dis[v] + u.second) {
        s.erase(make_pair(dis[u.first], u.first));
        dis[u.first] = dis[v] + u.second;
        par[u.first] = v;
        s.insert(make_pair(dis[u.first], u.first));
      }
    }
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  G.resize(n);
  for (int i = 0; i < m; ++i) {
    int x, y;
    long long z;
    scanf("%d%d%lld", &x, &y, &z);
    x--, y--;
    G[x].push_back(make_pair(y, z));
    G[y].push_back(make_pair(x, z));
  }
  dijkstra(0);
  if (dis[n - 1] == INF)
    printf("-1\n");
  else {
    stack<int> ans;
    ans.push(n - 1);
    int curr = n - 1;
    while (curr != 0) {
      curr = par[curr];
      ans.push(curr);
    }
    while (!ans.empty()) {
      int x = ans.top() + 1;
      ans.pop();
      printf("%d ", x);
    }
    printf("\n");
  }
  return 0;
}
