#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
int main() {
	ll n,m;
	cin>>n>>m;
	vector<vector<P>>G(n+1);
	for(int i = 0;i<m;i++){
		ll u,v,w;
		cin>>u>>v>>w;
		G[u].push_back({v,w});
		G[v].push_back({u,w});
	}
	
	ll src;
	cin>>src;
	priority_queue<P,vector<P>,greater<P>>pq;
	vector<ll>dist(n+1,1e18);
	pq.push({0,src});
	dist[src]=0;
	
	while(!pq.empty()){
		auto u = pq.top();
	     ll x = u.first;
	     ll y = u.second;
	     pq.pop();
	    if(x > dist[y]) continue;
	     for(auto v : G[y]){
	     	ll x1 = v.first;
	     	ll y1 = v.second;
	     	
	     	if(dist[x1] > dist[y]+y1){
	     		dist[x1] = dist[y]+y1;
	     		pq.push({dist[x1],x1});
	     	}
	     }
	}
	if(dist[n] == 1e18){
		cout<<-1;
	}else{
		cout<<dist[n];
	}
	return 0;
}