#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long oo = 1e18;
int n, m;
int a[N], b[N], d[N], pre[N];
long long f[N];
vector<int> v[N];
void Dijkstra() {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      pq;
  for (int i = 2; i <= n; i++) f[i] = oo;
  pq.push({0, 1});
  while (!pq.empty()) {
    pair<long long, int> t = pq.top();
    pq.pop();
    if (t.first != f[t.second]) continue;
    for (auto i : v[t.second]) {
      int z = a[i] == t.second ? b[i] : a[i];
      if (f[z] > f[t.second] + d[i]) {
        f[z] = f[t.second] + d[i];
        pre[z] = t.second;
        pq.push({f[z], z});
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a[i] >> b[i] >> d[i];
    v[a[i]].push_back(i);
    v[b[i]].push_back(i);
  }
  Dijkstra();
  if (f[n] == oo) return cout << -1, 0;
  stack<int> st;
  int x = n;
  while (x) {
    st.push(x);
    x = pre[x];
  }
  while (!st.empty()) cout << st.top() << ' ', st.pop();
  return 0;
}
