#include <bits/stdc++.h>
using namespace std;
const long long INF = 0x3f3f3f3f3f3f3f3f;
const int MAXI = 100005;
int n, m, vis[MAXI], path[MAXI], p[MAXI];
long long dis[MAXI];
struct Node {
  int to, cost;
};
vector<Node> v[MAXI];
void spfa() {
  fill(dis, dis + MAXI, INF);
  fill(vis, vis + MAXI, false);
  queue<int> qiu;
  qiu.push(1);
  dis[1] = 0;
  vis[1] = true;
  while (!qiu.empty()) {
    int x = qiu.front();
    qiu.pop();
    vis[x] = false;
    if (dis[x] > dis[n]) {
      continue;
    }
    int len = v[x].size();
    for (int i = 0; i < len; i++) {
      int to = v[x][i].to;
      if (dis[to] > dis[x] + v[x][i].cost) {
        dis[to] = dis[x] + v[x][i].cost;
        path[to] = x;
        if (!vis[to]) {
          vis[to] = true;
          qiu.push(to);
        }
      }
    }
  }
}
int main() {
  int a, b, w;
  Node k;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &w);
    k.to = b, k.cost = w;
    v[a].push_back(k);
    k.to = a;
    v[b].push_back(k);
  }
  spfa();
  if (dis[n] == INF) {
    cout << -1 << endl;
  } else {
    int j = 0;
    while (n) {
      p[j++] = n;
      n = path[n];
    }
    for (int i = j - 1; i >= 0; i--) cout << p[i] << (i == 0 ? '\n' : ' ');
  }
  return 0;
}
