#include <bits/stdc++.h>
using namespace std;
long long int n, m, a, b, c, arr[100005], nodee[100005];
vector<pair<long long int, long long int> > v[100005];
void printt(long long int p) {
  if (p != 1) printt(nodee[p]);
  cout << p << " ";
}
void solve() {
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  for (int i = 0; i <= n; i++) {
    arr[i] = INT64_MAX;
    nodee[i] = i;
  }
  priority_queue<long long int> q;
  q.push(1);
  arr[1] = 0;
  while (q.size()) {
    long long int node = q.top();
    q.pop();
    for (int i = 0; i < (long long int)v[node].size(); i++) {
      long long int next = v[node][i].first;
      long long int price = v[node][i].second;
      if (arr[node] + price < arr[next]) {
        arr[next] = arr[node] + price;
        nodee[next] = node;
        q.push(next);
      }
    }
  }
  if (arr[n] == INT64_MAX)
    cout << -1 << endl;
  else
    printt(n);
  cout << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
