#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    f |= (ch == '-');
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return f ? -x : x;
}
inline void Write(long long x) {
  if (x < 0) {
    x = -x;
  }
  if (x > 9) {
    Write(x / 10);
  }
  putchar((x % 10) ^ 48);
  return;
}
const int MAXN = 1e5 + 10;
bool Visited[MAXN];
long long Dist[MAXN];
vector<pair<int, long long> > Graph[MAXN];
int n, m, From, To, Value, Pre[MAXN];
inline void Dijkstra(int S) {
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >
      Q;
  for (register int i = 1; i <= n; ++i) {
    Dist[i] = 1LL << 60;
    Visited[i] = false;
    Pre[i] = i;
  }
  Dist[S] = 0, Q.push(make_pair(0, S));
  while (!Q.empty()) {
    S = Q.top().second, Q.pop();
    if (Visited[S]) {
      continue;
    }
    Visited[S] = true;
    for (register int i = 0; i ^ Graph[S].size(); ++i) {
      pair<long long, int> Child = Graph[S][i];
      if (Dist[Child.first] > Dist[S] + Child.second) {
        Dist[Child.first] = Dist[S] + Child.second, Pre[Child.first] = S;
        Q.push(make_pair(Dist[Child.first], Child.first));
      }
    }
  }
  return;
}
inline void Print(int Index) {
  if (Pre[Index] ^ Index) {
    Print(Pre[Index]);
  }
  Write(Index), putchar(' ');
  return;
}
signed main() {
  register int i;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (i = 1; i <= n; ++i) {
      Graph[i].clear();
    }
    while (m--) {
      From = Read(), To = Read(), Value = Read();
      Graph[From].push_back(make_pair(To, Value));
      Graph[To].push_back(make_pair(From, Value));
    }
    Dijkstra(1);
    if (Dist[n] == 1LL << 60) {
      printf("-1\n");
      continue;
    }
    Print(n), putchar('\n');
  }
  return 0;
}
