#include <bits/stdc++.h>
using namespace std;
const long long OO = 0x3f3f3f3f3f3f3f3f, N = 1e5 + 5, M = 1e5 + 5;
vector<pair<long long, long long>> adj[N];
long long n, m, c, src, u, v;
long long parent[N];
long long dis[N];
long long arr[N];
void dijkstra(int src) {
  memset(dis, OO, sizeof dis);
  dis[src] = 0;
  parent[src] = 0;
  priority_queue<pair<long long, long long>> q;
  q.push({0, src});
  while (!q.empty()) {
    long long u = q.top().second;
    long long uc = -q.top().first;
    q.pop();
    if (dis[u] != uc) continue;
    for (pair<long long, long long> neig : adj[u]) {
      int c = neig.first;
      int v = neig.second;
      if (dis[v] > dis[u] + c) {
        dis[v] = dis[u] + c;
        parent[v] = u;
        q.push({-dis[v], v});
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; ++i) {
    scanf("%d %d %d", &u, &v, &c);
    adj[u].push_back({c, v});
    adj[v].push_back({c, u});
  }
  dijkstra(1);
  int dist = n;
  if (dis[dist] >= OO) {
    printf("%d\n", -1);
  } else {
    long long sz = 0;
    arr[0] = dist;
    int i = 1;
    while (dist != 0) {
      dist = parent[dist];
      arr[i] = dist;
      sz++;
      i++;
    }
    for (int j = sz - 1; j > 0; --j) {
      printf("%d ", arr[j]);
    }
    printf("%d\n", n);
  }
  return 0;
}
