#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
long long dis[N];
vector<pair<int, long long>> g[N];
int par[N];
struct option {
  bool operator()(const pair<int, long long> &a,
                  const pair<int, long long> &b) const {
    return a.second > b.second;
  }
};
void solve(int s) {
  priority_queue<pair<int, long long>, vector<pair<int, long long>>, option> pq;
  pq.push({s, 0});
  dis[s] = 0;
  while (!pq.empty()) {
    int uf = pq.top().first;
    long long us = pq.top().second;
    pq.pop();
    if (dis[uf] != us) continue;
    for (auto v : g[uf]) {
      if (us + v.second < dis[v.first]) {
        dis[v.first] = us + v.second;
        par[v.first] = uf;
        pq.push({v.first, dis[v.first]});
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  for (int i = 1; i <= n; ++i) {
    dis[i] = LLONG_MAX;
  }
  solve(1);
  if (dis[n] == LLONG_MAX) {
    cout << "-1\n";
    return 0;
  }
  par[1] = -1;
  vector<int> ans;
  while (n != -1) {
    ans.push_back(n);
    n = par[n];
  }
  for (int i = ans.size() - 1; i >= 0; --i) cout << ans[i] << " ";
}
