#include <bits/stdc++.h>
using namespace std;
int p[200001];
long long d[200001];
void read(int &x) {
  char c = getchar();
  x = c - '0';
  c = getchar();
  while (c >= '0') {
    x = x * 10 + c - '0';
    c = getchar();
  }
}
vector<long long> e[200001];
bool used[200001];
int main() {
  long long INF = 1000000000000000000LL;
  int n, m, s, f;
  scanf("%d%d", &n, &m);
  s = 1;
  f = n;
  while (m--) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    e[a].push_back(b);
    e[a].push_back(c);
    e[b].push_back(a);
    e[b].push_back(c);
  }
  for (int i = 1; i <= n; i++) d[i] = INF;
  d[s] = 0;
  used[s] = 1;
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  int v = s;
  q.push(make_pair(0, s));
  while (q.size()) {
    v = q.top().second;
    q.pop();
    used[v] = 1;
    if (v == f) {
      int num = 0;
      while (v != s) {
        d[num++] = v;
        v = p[v];
      }
      d[num++] = s;
      printf("%d", d[num - 1]);
      for (int i = num - 2; i >= 0; i--) {
        printf(" %d", d[i]);
      }
      puts("");
      return 0;
    }
    long long se = INF;
    int x = -1;
    for (int i = 0; i < e[v].size(); i += 2) {
      int to = e[v][i];
      int t = e[v][i + 1];
      if (d[to] > d[v] + t) {
        d[to] = d[v] + t;
        p[to] = v;
        q.push(make_pair(d[to], to));
      }
    }
  }
  puts("-1");
  return 0;
}
