#include<iostream>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
using namespace std;
int N,M,T;
vector<pair<long,long> >f(int S,const vector<vector<pair<int,int> > >&G)
{
	vector<vector<long> >d(M+1,vector<long>(N,(long)1e18));
	d[0][S]=0;
	priority_queue<pair<long,pair<int,int> > >P;
	P.push(make_pair(0L,make_pair(0,S)));
	while(!P.empty())
	{
		long c=-P.top().first;
		int u=P.top().second.second,w=P.top().second.first;
		P.pop();
		if(d[w][u]<c)continue;
		for(pair<int,int>e:G[u])
		{
			int v=e.first;
			if(e.second==-1)
			{
				if(w<M&&d[w+1][v]>c)
				{
					d[w+1][v]=c;
					P.push(make_pair(-c,make_pair(w+1,v)));
				}
			}
			else
			{
				if(d[w][v]>c+e.second)
				{
					d[w][v]=c+e.second;
					P.push(make_pair(-c-e.second,make_pair(w,v)));
				}
			}
		}
	}
	deque<pair<long,long> >Q;
	for(long e=M;e>=0;e--)
	{
		long f=d[e][T];
		if(f==(long)1e18)continue;
		while(Q.size()>=2)
		{
			long a=Q[Q.size()-2].first,b=Q[Q.size()-2].second;
			long c=Q.back().first,d=Q.back().second;
			if((c-a)*(f-b)+(d-b)*(a-e)>=0)Q.pop_back();
			else break;
		}
		Q.push_back(make_pair(e,f));
	}
	return vector<pair<long,long> >(Q.begin(),Q.end());
}
int main()
{
	int S1,S2;
	while(cin>>N>>M>>S1>>S2>>T,N)
	{
		S1--,S2--,T--;
		vector<vector<pair<int,int> > >G(N);
		for(int i=0;i<M;i++)
		{
			int a,b;string w;
			cin>>a>>b>>w;
			a--,b--;
			if(w=="x")
			{
				G[a].push_back(make_pair(b,-1));
				G[b].push_back(make_pair(a,-1));
			}
			else
			{
				int W=0;
				for(char c:w)W=W*10+c-'0';
				G[a].push_back(make_pair(b,W));
				G[b].push_back(make_pair(a,W));
			}
		}
		vector<pair<long,long> >d1=f(S1,G);
		vector<pair<long,long> >d2=f(S2,G);
		long ans=9e18;
		long pre=0;
		int i1=0,i2=0;
		while(true)
		{
			long t1=1e15,t2=1e15;
			if(i1+1<d1.size())
			{
				long a=d1[i1].first,b=d1[i1].second;
				long c=d1[i1+1].first,d=d1[i1+1].second;
				t1=(d-b+(a-c)-1)/(a-c);
			}
			if(i2+1<d2.size())
			{
				long a=d2[i2].first,b=d2[i2].second;
				long c=d2[i2+1].first,d=d2[i2+1].second;
				t2=(d-b+(a-c)-1)/(a-c);
			}
			long l=pre,r=t1<t2?t1:t2;
			pre=max(pre,r--);
			long a=d1[i1].first,b=d1[i1].second;
			long c=d2[i2].first,d=d2[i2].second;
			while(r-l>3)
			{
				long m1=(l+l+r)/3,m2=(l+r+r)/3;
				if(abs(a*m1+b-(c*m1+d))<abs(a*m2+b-(c*m2+d)))r=m2;
				else l=m1;
			}
			for(long t=l;t<=r;t++)ans=min(ans,abs(a*t+b-(c*t+d)));
			if(t1<=t2)i1++;
			else i2++;
			if(i1==d1.size()||i2==d2.size())break;
		}
		cout<<ans<<endl;
	}
}

