#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> P;
vector<P> G[1111];
int dp1[111][1111];
int dp2[111][1111];
int INF=1LL<<45LL;
struct st{
  int x,v,d;
  st(){}
  st(int x,int v,int d):x(x),v(v),d(d){}
  bool operator<(const st a)const{
    return d>a.d;
  }
};
int n,m,s1,s2,t;
int ans;
int calc1(int i,int x){
  return dp1[i][t]+i*x;
}
int calc2(int i,int x){
  return dp2[i][t]+i*x;
}
int check1(int x){
  int res=-1;
  for(int i=0;i<111;i++){
    if(dp1[i][t]==INF) continue;
    if(res<0) res=i;
    if(calc1(i,x)<calc1(res,x)) res=i;
  }
  return res;
}
int check2(int x){
  int res=-1;
  for(int i=0;i<111;i++){
    if(dp2[i][t]==INF) continue;
    if(res<0) res=i;
    if(calc2(i,x)<calc2(res,x)) res=i;
  }
  return res;
}
int calc3(int x){
  return calc1(check1(x),x)-calc2(check2(x),x);
}
signed main(){
  while(cin>>n>>m>>s1>>s2>>t,n){
    s1--;s2--;t--;
    for(int i=0;i<1111;i++) G[i].clear(); 
    for(int i=0;i<m;i++){
      int a,b,w;
      string s;
      cin>>a>>b>>s;
      if(s=="x") w=-1;
      else w=stoll(s);
      a--;b--;
      G[a].push_back(P(b,w));
      G[b].push_back(P(a,w));
    }
    for(int i=0;i<111;i++){
      fill(dp1[i],dp1[i]+1111,INF);
      fill(dp2[i],dp2[i]+1111,INF);
    }
    priority_queue<st> q;
    q.emplace(0,s1,0);
    dp1[0][s1]=0;
    while(!q.empty()){
      st p=q.top();q.pop();
      if(dp1[p.x][p.v]<p.d) continue;
      if(p.x>105) continue;
      //cout<<p.x<<" "<<p.v<<" "<<p.d<<endl;
      for(P u:G[p.v]){
	int nv=u.first,nx=p.x+(~u.second?0:1),nd=p.d+(~u.second?u.second:0);
	if(dp1[nx][nv]<=nd) continue;
	dp1[nx][nv]=nd;
	q.emplace(nx,nv,nd);
      }
    }
    //puts("OK");
    q.emplace(0,s2,0);
    dp2[0][s2]=0;
    while(!q.empty()){
      st p=q.top();q.pop();
      if(dp2[p.x][p.v]<p.d) continue;
      if(p.x>105) continue;
      for(P u:G[p.v]){
	int nv=u.first,nx=p.x+(~u.second?0:1),nd=p.d+(~u.second?u.second:0);
	if(dp2[nx][nv]<=nd) continue;
	dp2[nx][nv]=nd;
	q.emplace(nx,nv,nd);
      }
    }
    //for(int i=0;i<11;i++) cout<<i<<":dp1:"<<dp1[i][t]<<endl;
    //for(int i=0;i<11;i++) cout<<i<<":dp2:"<<dp2[i][t]<<endl;
    
    vector<int> b;
    b.push_back(0);
    b.push_back(INF);
    int L=0;
    while(check1(L)!=check1(INF)){
      int l=L,r=INF;
      while(l+1<r){
	int mid=(l+r)/2;
	if(check1(L)!=check1(mid)) r=mid;
	else l=mid;
      }
      L=l+1;
      b.push_back(L);
    }
    L=0;
    while(check2(L)!=check2(INF)){
      int l=L,r=INF;
      while(l+1<r){
	int mid=(l+r)/2;
	if(check2(L)!=check2(mid)) r=mid;
	else l=mid;
      }
      L=l+1;
      b.push_back(L);
    }
    sort(b.begin(),b.end());
    b.erase(unique(b.begin(),b.end()),b.end());
    ans=INF;
    for(int i=0;i<(int)b.size();i++){
      ans=min(ans,abs(calc3(b[i])));
      if(i==(int)b.size()-1) break;
      int x=b[i],y=b[i+1]-1;
      ans=min(ans,abs(calc3(y)));
      //cout<<x<<" "<<y<<endl;
      //cout<<check1(x)<<" "<<check1(y)<<endl;
      //cout<<check2(x)<<" "<<check2(y)<<endl;
      if(x==y) continue;
      if(calc3(x)*calc3(y)<0){
	//cout<<calc3(x)<<"*"<<calc3(y)<<endl;
	int d=abs(calc3(x+1)-calc3(x));
	if(d==0) continue;
	int tmp=abs(calc3(x));
	//cout<<tmp<<":"<<d<<endl;
	ans=min(ans,abs(tmp-(tmp/d*d)));
	ans=min(ans,abs(((tmp/d+1)*d)-tmp));
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 8116