#include <bits/stdc++.h>
using namespace std;
long long dj[101003];
vector<pair<int, int> > adj[101003];
vector<int> res;
int r[100003];
int main() {
  int n, m, a, b, c;
  cin >> n >> m;
  priority_queue<pair<long long, int> > order;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b >> c;
    if (a == b) continue;
    adj[a].push_back(make_pair(c, b));
    adj[b].push_back(make_pair(c, a));
  }
  for (int i = 1; i <= n; i++) dj[i] = 1000000000000;
  dj[1] = 0;
  order.push(make_pair(0, 1));
  while (!order.empty()) {
    long long index = order.top().second, val = -(order.top().first);
    order.pop();
    if (dj[index] != val) {
      continue;
    }
    for (vector<pair<int, int> >::const_iterator i = adj[index].begin();
         i != adj[index].end(); ++i) {
      if ((val + i->first) < dj[i->second]) {
        order.push(make_pair(-(dj[i->second] = val + i->first), i->second));
        r[i->second] = index;
      }
    }
  }
  if (dj[n] == 1000000000000)
    cout << -1 << endl;
  else {
    int i = n;
    while (1) {
      res.push_back(i);
      if (i <= 1) break;
      i = r[i];
    }
    for (int i = res.size() - 1; i >= 0; i--) cout << res[i] << " ";
  }
}
