#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
typedef long long ll;
struct edge{
	int to;
	ll cost;
};
struct state{
	int mag;
	ll dist;
	int id;
/*	bool operator<(const state& s) const {
		if(mag!=s.mag) return mag<s.mag;
		if(dist!=s.dist) return dist<s.dist;
		return id<s.id;
	}*/
	bool operator>(const state& s) const {
		if(mag!=s.mag) return mag>s.mag;
		if(dist!=s.dist) return dist>s.dist;
		return id>s.id;
	}
};
int N,M,s1,s2,t,magic;
ll d[1000][101];
ll inf=1e18;
vector<edge> G[1000];
int To_int(string s){
	int ret=0;
	rep(i,s.size()){
		ret*=10;
		ret+=s[i]-'0';
	}
	return ret;
}
void dijkstra(int s){
	rep(i,N) rep(j,magic+1) d[i][j]=inf;
	priority_queue<state,vector<state>,greater<state> > que;
	d[s][0]=0;
	que.push(state{0,0,s});
	while(!que.empty()){
		state st=que.top();
		que.pop();
		int mag=st.mag;
		ll dis=st.dist;
		int v=st.id;
		if(d[v][mag]!=dis) continue;
		for(auto e:G[v]){
			if(e.cost==-1){
				if(mag==magic) continue;
				if(d[v][mag]<d[e.to][mag+1]){
					d[e.to][mag+1]=d[v][mag];
					que.push(state{mag+1,d[e.to][mag+1],e.to});
				}
			}else{
				if(d[v][mag]+e.cost<d[e.to][mag]){
					d[e.to][mag]=d[v][mag]+e.cost;
					que.push(state{mag,d[e.to][mag],e.to});
				}
			}
		}
	}
}
typedef double D;
typedef complex<D> P;
typedef pair<P,P> L;
D cro(P a,P b){return imag(conj(a)*b);}
bool ispal(L a,L b){
	return abs(cro(a.fs-a.sc,b.fs-b.sc))<1e-9;
}
P intLL(L a,L b){
	D t=cro(a.sc-a.fs,a.sc-b.fs)/cro(a.sc-a.fs,b.sc-b.fs);
	return b.fs+t*(b.sc-b.fs);
}
int main(){
	while(true){
		cin>>N>>M>>s1>>s2>>t;
		magic=0;
		s1--,s2--,t--;
		if(N==0) break;
		rep(i,N) G[i].clear();
		rep(i,M){
			int a,b;
			string w;
			cin>>a>>b>>w;
			a--,b--;
			if(w[0]=='x'){
				G[a].pb(edge{b,-1});
				G[b].pb(edge{a,-1});
				magic++;
			}else{
				int ww=To_int(w);
				G[a].pb(edge{b,ww});
				G[b].pb(edge{a,ww});
			}
		}
		dijkstra(t);
//		rep(i,magic+1) printf("d[s1][%d]=%lld\n",i,d[s1][i]);
//		rep(i,magic+1) printf("d[s2][%d]=%lld\n",i,d[s2][i]);

//		puts("done");
		vector<L> vla,nvla;
		vector<D> cha;
		vector<int> Aa,nAa;
		{
			ll mn=inf;
			rep(i,magic+1){
				if(mn<=d[s1][i]) continue;
				mn=d[s1][i];
				vla.pb(L(P(0,d[s1][i]),P(1,d[s1][i]+i)));
				Aa.pb(i);
			}
			reverse(all(vla));
			reverse(all(Aa));
			int K=vla.size();
			nvla.pb(vla[0]);
			nAa.pb(Aa[0]);
			rep1(i,K-1){
				int j=(int)nvla.size() -1;
				while(j>=0){
					D x=intLL(vla[i],nvla[j]).real();
					if(j==0||x>cha[j-1]){
						cha.pb(x);
						nvla.pb(vla[i]);
						nAa.pb(Aa[i]);
						break;
					}
					j--;
					nvla.pop_back();
					cha.pop_back();
					nAa.pop_back();
				}
			}
			vla=nvla,Aa=nAa;
		}
		vector<L> vlb,nvlb;
		vector<D> chb;
		vector<int> Ab,nAb;
		{
			ll mn=inf;
			rep(i,magic+1){
				if(mn<=d[s2][i]) continue;
				mn=d[s2][i];
				vlb.pb(L(P(0,d[s2][i]),P(1,d[s2][i]+i)));
				Ab.pb(i);
			}
			reverse(all(vlb));
			reverse(all(Ab));
			int K=vlb.size();
			nvlb.pb(vlb[0]);
			nAb.pb(Ab[0]);
			rep1(i,K-1){
				int j=(int)nvlb.size() -1;
				while(j>=0){
					D x=intLL(vlb[i],nvlb[j]).real();
					if(j==0||x>chb[j-1]){
						chb.pb(x);
						nvlb.pb(vlb[i]);
						nAb.pb(Ab[i]);
						break;
					}
					j--;
					nvlb.pop_back();
					chb.pop_back();
					nAb.pop_back();
				}
			}
			vlb=nvlb,Ab=nAb;
		}
/*		rep(i,vla.size()){
			cout<<vla[i].fs<<" "<<vla[i].sc<<endl;
		}
		puts("");
		rep(i,vlb.size()){
			cout<<vlb[i].fs<<" "<<vlb[i].sc<<endl;
		}
		rep(i,Aa.size()) show(Aa[i]);
		rep(i,Ab.size()) show(Ab[i]);*/
		vector<ll> xs;
		xs.pb(0);
		for(D x:cha) rep(i,3) xs.pb((ll)x+i-1);
		for(D x:chb) rep(i,3) xs.pb((ll)x+i-1);
		cha.pb(inf);
		chb.pb(inf);
		cha.insert(cha.begin(),0);
		chb.insert(chb.begin(),0);
/*		show(vla.size());
		show(vlb.size());
		show(cha.size());
		show(chb.size());*/
		rep(i,vla.size()) rep(j,vlb.size()){
			L a=vla[i],b=vlb[j];
			if(ispal(a,b)) continue;
			P p=intLL(a,b);
			D x=p.real();
			rep(i,11) xs.pb((ll)x+i-5);
		}
		ll ans=inf;
//		rep(i,chb.size()) show(chb[i]);
		sort(all(xs));
		xs.erase(unique(all(xs)),xs.end());
		for(ll x:xs){
			if(x<0||x>inf) continue;
			ll A=inf,B=inf;
			rep(i,vla.size()){
				ll a=Aa[i];
				ll b=d[s1][a];
				chmin(A,a*x+b);
			}
			rep(i,vlb.size()){
				ll a=Ab[i];
				ll b=d[s2][a];
				chmin(B,a*x+b);
			}
			chmin(ans,abs(A-B));
/*			int ida_=lower_bound(all(cha),x)-cha.begin()-1;
			ll A=inf;
			{
				for(int ida=ida_-1;ida<=ida_+1;ida++){
					if(ida<0||ida>=vla.size()) continue;
					ll a=Aa[ida];
					ll b=d[s1][a];
//					show(ida);
//					show(a);
//					show(b);
					chmin(A,a*x+b);
				}
			}
			int idb_=lower_bound(all(chb),x)-chb.begin()-1;
			ll B=inf;
			{
				for(int idb=idb_-1;idb<=idb_+1;idb++){
					if(idb<0||idb>=vlb.size()) continue;
					ll a=Ab[idb];
					ll b=d[s2][a];
//					show(idb);
//					show(a);
//					show(b);
					chmin(B,a*x+b);
				}
			}
			show(x);
			if(x==2){
			show(A);
			show(B);
			show(ida_);
			show(idb_);
			}
			chmin(ans,abs(A-B));*/
		}
		cout<<ans<<endl;
	}
}

Measured memory_bytes: 2256