#include <bits/stdc++.h>
using namespace std;
template <class T>
void dbs(string str, T t) {
  cerr << str << " : " << t << "\n";
}
template <class T, class... S>
void dbs(string str, T t, S... s) {
  long long idx = str.find(',');
  cerr << str.substr(0, idx) << " : " << t << ", ";
  dbs(str.substr(idx + 1), s...);
}
template <class T>
void prc(T a, T b) {
  cerr << "[";
  for (T i = a; i != b; ++i) {
    if (i != a) cerr << ", ";
    cerr << *i;
  }
  cerr << "]\n";
}
template <class T>
void prall(T a) {
  prc(a.begin(), a.end());
}
const int inf = 1000 * 1000 * 1000 + 9;
const long long infl = 1ll * inf * inf;
const int mod = 1000 * 1000 * 1000 + 7;
vector<vector<pair<int, int>>> adj;
map<pair<int, int>, int> edges;
struct S {
  int u, v;
  long long d;
  bool operator<(const S &a) const { return a.d < d; }
};
void solve() {
  int n, m;
  cin >> n >> m;
  adj.resize(n);
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--;
    v--;
    if (u != v) {
      if (u > v) swap(u, v);
      if (edges.find(make_pair(u, v)) == edges.end()) {
        edges[make_pair(u, v)] = w;
      } else
        edges[make_pair(u, v)] = min(w, edges[make_pair(u, v)]);
    }
  }
  for (auto p : edges) {
    int u = p.first.first, v = p.first.second, w = p.second;
    adj[u].push_back(make_pair(v, w));
    adj[v].push_back(make_pair(u, w));
  }
  vector<long long> dist(n, infl);
  priority_queue<S> q;
  vector<int> par(n);
  q.push({0, 0, 0});
  while (!q.empty()) {
    S s = q.top();
    q.pop();
    if (dist[s.v] > s.d) {
      dist[s.v] = s.d;
      par[s.v] = s.u;
      for (auto p : adj[s.v]) {
        q.push({s.v, p.first, p.second + s.d});
      }
    }
  }
  if (dist[n - 1] < infl) {
    vector<int> ans;
    int u = n - 1;
    while (1) {
      ans.push_back(u + 1);
      if (u == 0) break;
      u = par[u];
    }
    reverse(ans.begin(), ans.end());
    for (int v : ans) cout << v << " ";
  } else {
    cout << -1 << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout << fixed << setprecision(13);
  solve();
}
