#include <bits/stdc++.h>
using namespace std;
typedef pair<long long int, long long int> ipair;
inline void addedge(vector<vector<ipair> > &g, int &u, int &v, int &w) {
  g[u].push_back({v, w});
  g[v].push_back({u, w});
}
void Dijkstra(vector<vector<ipair> > &g, int &v, int source) {
  vector<long long int> dist(v, 10000000000000000);
  vector<bool> visited(v, false);
  map<long long int, long long int> Map;
  priority_queue<ipair, vector<ipair>, greater<ipair> > pq;
  pq.push({0, source});
  dist[source] = 0;
  while (!pq.empty()) {
    ipair front = pq.top();
    long long int vertex = front.second;
    pq.pop();
    for (auto it = g[vertex].begin(); it != g[vertex].end(); it++) {
      long long int adj_vertex = it->first;
      long long int weight = it->second;
      if (!visited[adj_vertex] && dist[adj_vertex] > dist[vertex] + weight) {
        dist[adj_vertex] = dist[vertex] + weight;
        pq.push({dist[adj_vertex], adj_vertex});
        Map[adj_vertex] = vertex;
      }
    }
    visited[vertex] = true;
  }
  if (dist[v - 1] != 10000000000000000) {
    int i = v - 1;
    vector<long long int> path;
    path.push_back(i + 1);
    while (i != source) {
      i = Map[i];
      path.push_back(i + 1);
    }
    for (int i = path.size() - 1; i >= 0; i--) {
      cout << path[i] << " ";
    }
  } else {
    cout << -1;
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int v, e;
  cin >> v >> e;
  vector<vector<ipair> > g(v);
  for (int i = 0; i < e; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    u--, v--;
    addedge(g, u, v, w);
  }
  Dijkstra(g, v, 0);
  return 0;
}
