#include <bits/stdc++.h>
using namespace std;
using ll = long long;
static inline ll readLL() {
ll x = 0; int c = getchar(); bool neg = false;
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') { neg = true; c = getchar(); }
while (c >= '0' && c <= '9') { x = x * 10 + (c - '0'); c = getchar(); }
return neg ? -x : x;
}
const int N = 2e5 + 5;
const ll oo = (ll)1e18;
int n, m, a, b;
int head_[N], nxt_[2 * N], to_[2 * N];
ll w_[2 * N];
int ecnt = 0;
inline void addEdge(int u, int v, ll w) {
to_[ecnt] = v; w_[ecnt] = w; nxt_[ecnt] = head_[u]; head_[u] = ecnt++;
}
struct FibHeapArr {
ll key[N];
int deg[N], par[N], child[N], left[N], right[N];
bool mark[N], inHeap[N];
int mn = 0;
void init(int sz) {
mn = 0;
for (int i = 0; i <= sz; i++) {
par[i] = child[i] = 0;
left[i] = right[i] = i;
deg[i] = 0; mark[i] = false; inHeap[i] = false;
key[i] = oo;
}
}
inline void listRemove(int x) {
left[right[x]] = left[x];
right[left[x]] = right[x];
left[x] = right[x] = x;
}
inline void addRoot(int x) {
par[x] = 0; mark[x] = false;
if (!mn) { mn = x; left[x] = right[x] = x; return; }
left[x] = mn; right[x] = right[mn];
left[right[mn]] = x; right[mn] = x;
if (key[x] < key[mn]) mn = x;
}
inline bool empty() const { return mn == 0; }
void insert(int x, ll k) {
key[x] = k; deg[x] = 0; mark[x] = false;
par[x] = child[x] = 0; left[x] = right[x] = x;
inHeap[x] = true;
addRoot(x);
}
inline void link(int x, int y) {
listRemove(x);
par[x] = y; mark[x] = false;
if (!child[y]) { child[y] = x; left[x] = right[x] = x; }
else {
int c = child[y];
left[x] = c; right[x] = right[c];
left[right[c]] = x; right[c] = x;
}
deg[y]++;
}
int degTable[40];
void consolidate() {
int cnt = 0;
int p = mn;
static int roots[N];
do { roots[cnt++] = p; p = right[p]; } while (p != mn);
memset(degTable, 0, sizeof(degTable));
for (int i = 0; i < cnt; i++) {
int x = roots[i];
int d = deg[x];
while (degTable[d]) {
int y = degTable[d];
if (key[x] > key[y]) swap(x, y);
link(y, x);
degTable[d] = 0;
d = deg[x];
}
degTable[d] = x;
}
mn = 0;
for (int i = 0; i < 40; i++) if (degTable[i]) {
int x = degTable[i];
left[x] = right[x] = x;
addRoot(x);
}
}
pair<int, ll> popMin() {
int z = mn;
pair<int, ll> res = {z, key[z]};
if (child[z]) {
int c = child[z];
vector<int> kids;
int p = c;
do { kids.push_back(p); p = right[p]; } while (p != c);
for (int x : kids) { listRemove(x); addRoot(x); }
}
child[z] = 0; deg[z] = 0;
if (right[z] == z) mn = 0;
else { int p = right[z]; listRemove(z); mn = p; }
inHeap[z] = false;
if (mn) consolidate();
return res;
}
void cut(int x, int y) {
if (right[x] == x) child[y] = 0;
else { if (child[y] == x) child[y] = right[x]; listRemove(x); }
deg[y]--;
addRoot(x);
}
void cascadingCut(int x) {
int y = par[x];
if (!y) return;
if (!mark[x]) mark[x] = true;
else { cut(x, y); cascadingCut(y); }
}
void decreaseKey(int x, ll k) {
key[x] = k;
int y = par[x];
if (y && key[x] < key[y]) { cut(x, y); cascadingCut(y); }
if (key[x] < key[mn]) mn = x;
}
};
FibHeapArr pq;
ll dista[N];
int trace_[N];
vector<int> path;
void sol(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> a >> b;
memset(head_, -1, sizeof(int) * (n + 1));
for (int i = 0; i < m; i++) {
int u, v; ll w; cin >> u >> v >> w;
addEdge(u, v, w);
addEdge(v, u, w);
}
fill(dista, dista + n + 1, oo);
fill(trace_, trace_ + n + 1, -1);
pq.init(n);
dista[a] = 0;
pq.insert(a, 0);
while (!pq.empty()) {
auto [u, du] = pq.popMin();
if (u == b) break;
for (int e = head_[u]; e != -1; e = nxt_[e]) {
int v = to_[e];
ll nd = du + w_[e];
if (nd < dista[v]) {
dista[v] = nd;
trace_[v] = u;
if (!pq.inHeap[v]) pq.insert(v, nd);
else pq.decreaseKey(v, nd);
}
}
}
cout << (dista[b] == oo ? 0 : dista[b]);
}
int main() {
sol();
return 0;
}