#include <bits/stdc++.h>
using namespace std;
long long min(long long x, long long y) { return (x < y) ? x : y; }
long long max(long long x, long long y) { return (x > y) ? x : y; }
const int N = 100009;
set<pair<int, int> > c;
int n, m, u, w, x, y, r, l, g, t;
long long pre[N], d[N];
vector<pair<int, int> > v[N];
pair<int, int> p;
vector<int> ans;
void solve(int x, int y) {
  for (int i = 0; i < v[y].size(); i++)
    if (d[y] + v[y][i].second < d[v[y][i].first]) {
      c.erase(make_pair(d[v[y][i].first], v[y][i].first));
      d[v[y][i].first] = d[y] + v[y][i].second;
      pre[v[y][i].first] = y;
      c.insert(make_pair(d[v[y][i].first], v[y][i].first));
    }
  c.erase(make_pair(x, y));
}
int main() {
  cin >> n >> m;
  u = 0;
  w = n - 1;
  for (int i = 0; i < m; i++) {
    cin >> x >> y >> g;
    x--;
    y--;
    v[x].push_back(make_pair(y, g));
    v[y].push_back(make_pair(x, g));
  }
  for (int i = 0; i < n; i++) d[i] = 100000000009;
  d[u] = 0;
  c.insert(make_pair(d[u], u));
  for (int i = 0; i < n; i++) {
    p = *c.begin();
    solve(p.first, p.second);
  }
  t = w;
  while (t != u) {
    ans.push_back(t + 1);
    t = pre[t];
  }
  if (d[w] == 100000000009) {
    cout << -1;
    return 0;
  }
  ans.push_back(u + 1);
  for (int i = 0; i < ans.size(); i++) cout << ans[ans.size() - i - 1] << ' ';
  return 0;
}
