#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define INF 0x3f3f3f3f
template <typename... T>
void read(T &...args)
{
	((cin >> args), ...);
}
template <typename... T>
void write(T... args)
{
	((cout << args), ...);
}
const vector<pair<int, int>> nb = {{-1, -1}, {-1, 1}, {1, 1}, {1, -1}, {-1, 0}, {1, 0}, {0, 1}, {0, -1}};
const vector<pair<int, int>> nc = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); }
const int maxN = 200001;

vector<pii> adj[maxN];
int n, m, u, v;

void bfs(int src){
	queue<pii>q;
	vector<ll> dist(maxN, INF);
	q.push(mp(0, src));
	dist[src] = 0;
	int curr = 0;

	while (!q.empty()){

		int r = q.front().second;
		q.pop();
		for (auto i : adj[r]){
			int l = i.first;
			int weight = i.second;
			if (dist[l] > dist[r] + weight){
				if((!curr && weight) || (!weight && curr))curr=!curr;
				dist[l] = dist[r] + weight + curr;
				q.push(mp(dist[l], l));
			}
		}
	}
	write(dist[v],"\n");
}

void solve(){
	read(u, v);
	bfs(u);
}

int main(void){
	ios::sync_with_stdio(0);
	cin.tie(0);
	read(n, m);
	int a, b, t;
	for (int i = 0; i < m; ++i){
		read(a, b, t);
		adj[a].pb(mp(b,t));
		adj[b].pb(mp(a,t));
	}
	solve();
	return 0;
}