#include <bits/stdc++.h>
using namespace std;
long long int n, m;
vector<pair<long long int, long long int>> edges[100005];
unsigned long long int dis[100005];
bool vis[100005];
bool dfs(long long int node) {
  vis[node] = true;
  if (node == n) return true;
  for (auto i : edges[node]) {
    if (!vis[i.first])
      if (dfs(i.first)) return true;
  }
  return false;
}
void solve() {
  cin >> n >> m;
  for (long long int i = 0; i < m; i++) {
    long long int x, y, z;
    cin >> x >> y >> z;
    edges[x].push_back({y, z});
    edges[y].push_back({x, z});
  }
  memset(vis, false, sizeof(vis));
  bool canexist = dfs(1);
  if (canexist) {
    memset(vis, false, sizeof(vis));
    long long int prev[100005];
    for (long long int i = 1; i <= n; i++)
      dis[i] = 100000000007, prev[i] = i - 1;
    dis[1] = 0;
    priority_queue<pair<long long int, long long int>> q;
    q.push({0, 1});
    prev[1] = 0;
    while (!q.empty()) {
      pair<long long int, long long int> x = q.top();
      q.pop();
      if (!vis[x.second]) {
        vis[x.second] = true;
        for (auto i : edges[x.second]) {
          if (dis[i.first] >= dis[x.second] + i.second) {
            prev[i.first] = x.second;
            dis[i.first] = dis[x.second] + i.second;
            q.push({-dis[i.first], i.first});
          }
        }
      }
    }
    vector<long long int> ans;
    ans.push_back(n);
    long long int x = prev[n];
    while (x > 1) {
      ans.push_back(x);
      x = prev[x];
    }
    ans.push_back(1);
    reverse(ans.begin(), ans.end());
    for (auto i : ans) {
      cout << i << ' ';
    }
  } else {
    cout << -1;
  }
}
int32_t main() {
  {
    ios_base ::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  };
  long long int t = 1;
  while (t--) {
    solve();
    cout << "\n";
  }
  return 0;
}
