#include <bits/stdc++.h>
using namespace std;
struct Edge {
int dest;
long long weight;
};
int main() {
int n, m, s, t;
cin >> n >> m >> s >> t;
vector<vector<Edge>> g(n + 1);
g.reserve(n + 1);
while(m--){
int u, v;
long long c;
cin >> u >> v >> c;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
const long long INF = 1e18;
priority_queue<pair<long long, int>, vector<pair<long long, int>>, greater<pair<long long, int>>> pq;
vector<long long> dis(n + 1, INF);
vector<int> prev(n + 1, -1);
pq.push({0, s});
dis[s] = 0;
while(!pq.empty()){
int u = pq.top().second;
long long d = pq.top().first;
pq.pop();
if(d > dis[u]) continue;
for(const auto& edge : g[u]){
int v = edge.dest;
long long w = edge.weight;
if(dis[v] > dis[u] + w){
dis[v] = dis[u] + w;
pq.push({dis[v], v});
prev[v] = u;
}
}
}
if(dis[t] == INF){
cout << -1;
return 0;
}
vector<int> path;
for(int v = t; v != -1; v = prev[v]){
path.push_back(v);
}
reverse(path.begin(), path.end());
cout << dis[t] << '\n';
for(int node : path){
cout << node << ' ';
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgRWRnZSB7CiAgICBpbnQgZGVzdDsKICAgIGxvbmcgbG9uZyB3ZWlnaHQ7Cn07CgppbnQgbWFpbigpIHsKICAgIGludCBuLCBtLCBzLCB0OwogICAgY2luID4+IG4gPj4gbSA+PiBzID4+IHQ7CiAgICB2ZWN0b3I8dmVjdG9yPEVkZ2U+PiBnKG4gKyAxKTsKICAgIGcucmVzZXJ2ZShuICsgMSk7CiAgICB3aGlsZShtLS0pewogICAgICAgIGludCB1LCB2OwogICAgICAgIGxvbmcgbG9uZyBjOwogICAgICAgIGNpbiA+PiB1ID4+IHYgPj4gYzsKICAgICAgICBnW3VdLnB1c2hfYmFjayh7diwgY30pOwogICAgICAgIGdbdl0ucHVzaF9iYWNrKHt1LCBjfSk7CiAgICB9CiAgICBjb25zdCBsb25nIGxvbmcgSU5GID0gMWUxODsKICAgIHByaW9yaXR5X3F1ZXVlPHBhaXI8bG9uZyBsb25nLCBpbnQ+LCB2ZWN0b3I8cGFpcjxsb25nIGxvbmcsIGludD4+LCBncmVhdGVyPHBhaXI8bG9uZyBsb25nLCBpbnQ+Pj4gcHE7CiAgICB2ZWN0b3I8bG9uZyBsb25nPiBkaXMobiArIDEsIElORik7CiAgICB2ZWN0b3I8aW50PiBwcmV2KG4gKyAxLCAtMSk7IAogICAgcHEucHVzaCh7MCwgc30pOwogICAgZGlzW3NdID0gMDsKICAgIHdoaWxlKCFwcS5lbXB0eSgpKXsKICAgICAgICBpbnQgdSA9IHBxLnRvcCgpLnNlY29uZDsKICAgICAgICBsb25nIGxvbmcgZCA9IHBxLnRvcCgpLmZpcnN0OwogICAgICAgIHBxLnBvcCgpOwogICAgICAgIGlmKGQgPiBkaXNbdV0pIGNvbnRpbnVlOwogICAgICAgIGZvcihjb25zdCBhdXRvJiBlZGdlIDogZ1t1XSl7CiAgICAgICAgICAgIGludCB2ID0gZWRnZS5kZXN0OwogICAgICAgICAgICBsb25nIGxvbmcgdyA9IGVkZ2Uud2VpZ2h0OwogICAgICAgICAgICBpZihkaXNbdl0gPiBkaXNbdV0gKyB3KXsKICAgICAgICAgICAgICAgIGRpc1t2XSA9IGRpc1t1XSArIHc7CiAgICAgICAgICAgICAgICBwcS5wdXNoKHtkaXNbdl0sIHZ9KTsKICAgICAgICAgICAgICAgIHByZXZbdl0gPSB1OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogICAgaWYoZGlzW3RdID09IElORil7CiAgICAgICAgY291dCA8PCAtMTsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIHZlY3RvcjxpbnQ+IHBhdGg7CiAgICBmb3IoaW50IHYgPSB0OyB2ICE9IC0xOyB2ID0gcHJldlt2XSl7CiAgICAgICAgcGF0aC5wdXNoX2JhY2sodik7CiAgICB9CiAgICByZXZlcnNlKHBhdGguYmVnaW4oKSwgcGF0aC5lbmQoKSk7CiAgICBjb3V0IDw8IGRpc1t0XSA8PCAnXG4nOwogICAgZm9yKGludCBub2RlIDogcGF0aCl7CiAgICAgICAgY291dCA8PCBub2RlIDw8ICcgJzsKICAgIH0KICAgIHJldHVybiAwOwp9Cg==