#include <bits/stdc++.h>
using namespace std;
int MOD(int a, int b) {
  if (a > b)
    return a - b;
  else
    return b - a;
}
long long int max3(long long int a, long long int b, long long int c) {
  return max(c, max(a, b));
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int max(long long int a, long long int b) { return (a > b) ? a : b; }
long long int min(long long int a, long long int b) { return (a < b) ? a : b; }
long long int gcd(long long int a, long long int b) {
  if (b == 1)
    return 1;
  else if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}
bool vis[100005];
long long int dis[100005];
vector<pair<int, int>> adj[100005];
int par[100005];
void bfs(int s) {
  multiset<pair<long long int, int>> ms;
  ms.insert({0, s});
  vector<pair<int, int>>::iterator i;
  while (!ms.empty()) {
    pair<long long int, int> a = *(ms.begin());
    int x = a.second;
    long long int w = a.first;
    ms.erase(ms.begin());
    if (vis[x]) {
      continue;
    }
    vis[x] = true;
    for (i = adj[x].begin(); i < adj[x].end(); i++) {
      if (w + i->second < dis[i->first]) {
        dis[i->first] = i->second + w;
        par[i->first] = x;
        ms.insert({dis[i->first], i->first});
      }
    }
  }
}
bool func(pair<int, int> a, pair<int, int> b) {
  if (a.second < b.second)
    return true;
  else
    return false;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  memset(vis, 0, sizeof(vis));
  int n, m1, i;
  cin >> n >> m1;
  for (i = 1; i < n + 1; i++) {
    dis[i] = 1000000000000;
  }
  map<pair<int, int>, int> m;
  int j, k, w;
  for (i = 0; i < m1; i++) {
    cin >> j >> k >> w;
    if (j == k) continue;
    if (m[make_pair(min(j, k), max(j, k))] == 0) {
      m[make_pair(min(j, k), max(j, k))] = w;
    } else {
      if (w < m[make_pair(min(j, k), max(j, k))])
        m[make_pair(min(j, k), max(j, k))] = w;
    }
  }
  map<pair<int, int>, int>::iterator it;
  for (it = m.begin(); it != m.end(); it++) {
    adj[it->first.first].push_back(make_pair(it->first.second, it->second));
    adj[it->first.second].push_back(make_pair(it->first.first, it->second));
  }
  dis[1] = 0;
  par[1] = -1;
  bfs(1);
  if (dis[n] == 1000000000000)
    cout << "-1\n";
  else {
    int s = n;
    vector<int> v;
    while (s != -1) {
      v.push_back(s);
      s = par[s];
    }
    reverse(v.begin(), v.end());
    for (i = 0; i < v.size(); i++) {
      cout << v[i] << " ";
    }
    cout << "\n";
  }
  return 0;
}
