#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
const int INF = 2147483647;
const long long LINF = 9223372036854775807;
const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int n, m, pre[100005], dis[100005];
bool vis[100005];
vector<pair<int, int> > adj[100005];
set<pair<int, int> > st;
void out(int i) {
  if (i != 0) out(pre[i]);
  cout << i + 1 << ' ';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int(i) = 0; i < (m); i++) {
    int x, y, d;
    cin >> x >> y >> d;
    x--;
    y--;
    adj[x].push_back(make_pair(y, d));
    adj[y].push_back(make_pair(x, d));
  }
  st.insert(make_pair(0, 0));
  for (int i = 1; i < n; i++) dis[i] = INF;
  while (!st.empty()) {
    int i = st.begin()->second;
    int d = st.begin()->first;
    st.erase(st.begin());
    vis[i] = 1;
    for (int(j) = 0; j < (adj[i].size()); j++) {
      int to = adj[i][j].first;
      if (!vis[to] && d + adj[i][j].second < dis[to]) {
        st.erase(make_pair(dis[to], to));
        pre[to] = i;
        dis[to] = d + adj[i][j].second;
        st.insert(make_pair(dis[to], to));
      }
    }
  }
  if (dis[n - 1] == INF)
    cout << -1;
  else
    out(n - 1);
  return 0;
}
