#include<cstdio>
#include<iostream>
#define ll long long
#define abs(x) (x > 0 ? x : -(x))
#define F(a,b,c) ((a) + (b) * (c))
using namespace std;

const int maxn = 1200;
const int maxm = 2200;
const ll inf1 = 1000000000ll * 1100;
const ll inf = (1ll<<60);
int n,m,S1,S2,T,tot;
char ss[110];
int get(char *ss)
{
    int ret = 0;
    for (int i = 0; ss[i]; i++) {
        ret = ret * 10 + ss[i] - '0';
    }
    return ret;
}
ll a[2][110],b[2][110],f[2][110];
struct EE
{
    int to,ne;
    ll w;
    bool f;
}e[maxm * 2];
int L,head[maxn];
void adde(int u,int v,ll w)
{
    e[L].to = v;
    e[L].w = w;
    e[L].f = false;
    if (w == -1) e[L].f = true;
    e[L].ne = head[u];
    head[u] = L++;
}
bool vis[maxn][maxn];
ll dis[2][maxn][maxn];
int que[maxn * maxn][2];
void spfa(int s,int t,int tt)
{
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= tot; j++) {
            dis[tt][i][j] = inf;
            vis[i][j] = false;
        }
    vis[s][0] = true;
    dis[tt][s][0] = 0;
    int fron,rear;
    fron = rear = 0;
    que[rear][0] = s;
    que[rear][1] = 0;
    rear++;
    while (fron != rear) {
        int u1,u2;
        u1 = que[fron][0];
        u2 = que[fron][1];
        vis[u1][u2] = false;
        fron++;
      //  cout<<fron<<' '<<rear<<' '<<u1<<' '<<u2<<endl;
        if (fron == maxn * maxn) fron = 0;
        for (int i = head[u1]; i != -1; i = e[i].ne) {
            int p = e[i].f,v = e[i].to;
            ll w;
            if (u2 + p > tot) continue;
            if (p == 1) w = 0;
            else w = e[i].w;
            if (dis[tt][v][u2 + p] > dis[tt][u1][u2] + w) {
                dis[tt][v][u2 + p] = dis[tt][u1][u2] + w;
                if (!vis[v][u2 + p]) {
                    vis[v][u2 + p] = true;
                    que[rear][0] = v;
                    que[rear][1] = u2 + p;
                    rear++;
                    if (rear == maxn * maxn) rear = 0;
                }
            }
        }
    }
}
int cal(int s,int t,ll x,int tt)
{
    for (int i = 0; i < L; i++)
    if (e[i].f)
        e[i].w = x;
    //spfa(s,t);
    ll w = inf;
    int ret;
    for (int i = 0; i <= tot; i++)
        if (dis[tt][t][i] + i * x <= w) {
            w = dis[tt][t][i] + i * x;
            ret = i;
        }
    return ret;
}
void get(int s,int t,ll *f,ll *a,ll *b,int tt)
{
    ll R = inf1;
    ll now = -1;
    int m = 0;
    while (R >= 0) {
        ll l = 0,r = R;
        while (l < r) {
            ll mid = (l + r + 1) / 2;
            if (cal(s,t,mid,tt) == now)
                r = mid - 1;
            else
                l = mid;
          //  cout<<l<<endl;
        }
       // cout<<l<<endl;
        ll x = cal(s,t,l,tt);
        if (x == now) {
            f[m++] = -1;
            R = -1;
        }
        else {
            now = x;
            a[m] = dis[tt][T][now];
            b[m] = now;
            f[m++] = l;
            R = l;
        }
    }
}
ll cal2(ll a0,ll b0,ll a1,ll b1,ll tmax,ll tmin)
{
    ll l,r;
    l = tmin,r = tmax;
    while (l + 10 < r) {
        ll mid1 = l + (r - l) / 3;
        ll mid2 = l + (r - l) * 2 / 3;
        if (abs(F(a0,b0,mid1) - F(a1,b1,mid1)) > abs(F(a0,b0,mid2) - F(a1,b1,mid2)))
            l = mid1;
        else
            r = mid2;
    }
  //  cout<<l<<' '<<r<<endl;
    ll ret = inf;
    for (ll i = l; i <= r; i++) {
        //cout<<a0<<' '<<b0<<' '<<a1<<' '<<b1<<endl;
        ret = min(ret,abs(F(a0,b0,i) - F(a1,b1,i)));
    }
    return ret;
}
int main()
{
    while (scanf("%d%d%d%d%d",&n,&m,&S1,&S2,&T) != EOF) {
        if (n == 0 && m == 0) return 0;
        S1--;S2--;T--;
        tot = 0;
        L = 0;
        for (int i = 0; i < n; i++) head[i] = -1;
        for (int i = 0; i < m; i++) {
            int a,b,c;
            scanf("%d%d%s",&a,&b,ss);
            a--;
            b--;
            if (ss[0] == 'x') {
                adde(a,b,-1);
                adde(b,a,-1);
                tot++;
            }
            else {
                c = get(ss);
                adde(a,b,c);
                adde(b,a,c);
            }
        }
        for (int i = 0; i <= tot; i++)
            f[0][i] = f[1][i] = -1;
        spfa(S1,T,0);
        spfa(S2,T,1);
        get(S1,T,f[0],a[0],b[0],0);
        get(S2,T,f[1],a[1],b[1],1);
        //continue;
       // return 0;
        int i,j;
        i = 0,j = 0;
        ll ret = inf1;
        //for (int i = 0; i <= 10; i++)
       //     cout<<f[0][i]<<' '<<a[0][i]<<' '<<b[0][i]<<' '<<f[1][i]<<' '<<a[1][i]<<' '<<b[1][i]<<endl;
        while (1) {
            //cout<<i<<' '<<j<<endl;
            ret = min(ret,cal2(a[0][i],b[0][i],a[1][j],b[1][j],min(f[0][i],f[1][j]),max(f[0][i + 1],f[1][j + 1]) + 1));
            if (f[0][i + 1] == -1 && f[1][j + 1] == -1) break;
            //cout<<'x'<<endl;
            if (f[0][i + 1] > f[1][j + 1])
                i++;
            else
                j++;
        }
        cout<<ret<<endl;
    }
}

Measured memory_bytes: 30104