#include <bits/stdc++.h>
using namespace std;
long long dj[100003];
vector<pair<int, int> > adj[100003];
priority_queue<pair<long long, int> > order;
int r[100003];
void ans(int n) {
  if (n > 1) ans(r[n]);
  cout << n << " ";
}
int main() {
  int n, m, a, b, c;
  cin >> n >> m;
  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] = LONG_LONG_MAX;
  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 (int i = 0; i < adj[index].size(); i++) {
      int index2 = adj[index][i].second, val2 = adj[index][i].first;
      if (dj[index2] > val + val2)
        dj[index2] = (val + val2), order.push(make_pair(-dj[index2], index2)),
        r[index2] = index;
    }
  }
  if (dj[n] == LONG_LONG_MAX)
    cout << -1 << endl;
  else
    ans(n);
}
