#include <bits/stdc++.h>
using namespace std;
vector<long long> adj[100005];
map<pair<long long, long long>, long long> mp;
priority_queue<long long> q;
long long d[100005], ans[100005];
void print(int n) {
  if (n != 1) print(ans[n]);
  cout << n << " ";
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b, w;
    scanf("%d%d%d", &a, &b, &w);
    adj[a].push_back(b);
    adj[b].push_back(a);
    mp[{a, b}] = w;
    mp[{b, a}] = w;
  }
  q.push(1);
  for (int i = 2; i <= n; i++) d[i] = 1e18;
  d[1] = 0;
  while (!q.empty()) {
    long long u = q.top();
    q.pop();
    for (int i = 0; i < adj[u].size(); i++) {
      long long z = adj[u][i];
      long long w = mp[{u, z}];
      if (d[u] + w < d[z]) d[z] = d[u] + w, ans[z] = u, q.push(z);
    }
  }
  if (d[n] == 1e18) {
    cout << -1 << endl;
    return 0;
  }
  print(n);
  return 0;
}
