#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod/300;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acos(-1.0);

ll mod_pow(ll x, ll n, ll m) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 22;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	while (b) {
		ll r = a % b; a = b; b = r;
	}
	return a;
}
using speP = pair<ll, P>;
struct edge {
	int to, cost;
};
ll dist[1000][101];
int n, m, s1, s2, t;
void solve() {
	s1--; s2--; t--;
	vector<vector<edge>> G(n);
	rep(i, m) {
		int a, b; string s;
		cin >> a >> b >> s; a--; b--;
		int c;
		if (s == "x")c = -1;
		else c = stoi(s);
		G[a].push_back({ b,c });
		G[b].push_back({ a,c });
	}
	rep(i, n)rep(j, 101) {
		dist[i][j] = INF;
	}
	dist[t][0] = 0;
	priority_queue<speP, vector<speP>, greater<speP>> q;
	q.push({ 0,{t,0} });
	while (!q.empty()) {
		speP p = q.top(); q.pop();
		int id = p.second.first, k = p.second.second;
		if (dist[id][k] < p.first)continue;
		for (edge e : G[id]) {
			ll nd = p.first;
			int nk = k;
			if (e.cost < 0)nk++;
			else nd += e.cost;
			if (nk <= 100) {
				if (nd < dist[e.to][nk]) {
					dist[e.to][nk] = nd;
					q.push({ nd,{e.to,nk} });
				}
			}
		}
	}
	auto check = [&](LP x, LP y, LP z)->bool {
		return x.first * z.second-x.second*z.first <= (z.second-x.second) * y.first + y.second * (x.first - z.first);
	};
	auto calc = [&](LP x, LP y)->ll {
		ll dy = y.second - x.second;
		ll dx = x.first - y.first;
		return (dy + dx - 1) / dx;
	};
	vector<LP> v1, v2;
	for(int i = 100; i >= 0; i--) {
		if (dist[s1][i] == INF)continue;
		LP p = { i,dist[s1][i] };
		while (true) {
			bool f = true;
			if (v1.size() >= 2 && check(v1[v1.size() - 2], v1.back(), p)) {
				v1.pop_back(); f = false;
			}
			if (v1.size() && v1.back().second >= p.second) {
				v1.pop_back(); f = false;
			}
			if (f)break;
		}
		v1.push_back(p);
	}
	for (int i = 100; i >= 0; i--) {
		if (dist[s2][i] == INF)continue;
		LP p = { i,dist[s2][i] };
		while (true) {
			bool f = true;
			if (v2.size() >= 2 && check(v2[v2.size() - 2], v2.back(), p)) {
				v2.pop_back(); f = false;
			}
			if (v2.size() && v2.back().second >= p.second) {
				v2.pop_back(); f = false;
			}
			if (f)break;
		}
		v2.push_back(p);
	}
	/*cout << "?\n";
	rep(i, v1.size())cout << v1[i].first << " " << v1[i].second << "\n";
	cout << "?\n";
	rep(i, v2.size())cout << v2[i].first << " " << v2[i].second << "\n";*/
	vector<ll> d1, d2;
	d1.push_back(0); d2.push_back(0);
	rep(i, v1.size() - 1) {
		d1.push_back(calc(v1[i], v1[i + 1]));
	}
	rep(i, v2.size() - 1) {
		d2.push_back(calc(v2[i], v2[i + 1]));
	}
	int id[2] = { -1,-1 };
	vector<LP> v;
	rep(i, d1.size()) {
		v.push_back({ d1[i],0 });
	}
	rep(i, d2.size()) {
		v.push_back({ d2[i],1 });
	}
	sort(all(v));
	vector<P> vs;
	vector<ll> vals;
	rep(i, v.size()) {
		id[v[i].second]++;
		while (i + 1 < v.size() && v[i].first == v[i + 1].first) {
			i++; id[v[i].second]++;
		}
		vs.push_back({ id[0],id[1] });
		vals.push_back(v[i].first);
	}
	vals.push_back(INF);
	ll ans = INF;
	rep(j, vs.size()) {
		ll lx = vals[j], rx = vals[j + 1];
		int l = vs[j].first, r = vs[j].second;
		//cout << l << " " << r << " "<<lx<<" "<<rx<<"\n";
		ll cl = v1[l].first * lx + v1[l].second;
		ll cr = v2[r].first * lx + v2[r].second;
		ll dif = cl - cr;
		ll dx = v1[l].first - v2[r].first;
		if (dx == 0) {
			ans = min(ans, abs(cl - cr));
		}
		else if (dx > 0) {
			if (dif >= 0) {
				ans = min(ans, abs(cl - cr));
			}
			else {
				ll d = -dif / dx;
				d = min(d, rx - lx - 1);
				for (int i = -1; i <= 1; i++) {
					if (d + i < rx - lx&&d+i>=0) {
						ans = min(ans, abs(dif + dx * (d+i)));
					}
				}
			}
		}
		else {
			if (dif <= 0) {
				ans = min(ans, abs(cl - cr));
			}
			else {
				//cout << "hoge " << dif << " " << -dx << "\n";
				ll d = dif / -dx;
				d = min(d, rx - lx - 1);
				for (int i = -1; i <= 1; i++) {
					if (d + i < rx - lx&&d+i>=0) {
						ans = min(ans, abs(dif + dx * (d+i)));
					}
				}
			}
		}
	}
	//cout << "ans is ";
	cout << ans << "\n";
}




signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//init_f();
	//expr();
	//int t; cin >> t; rep(i, t)
	while (cin >> n >> m>>s1>>s2>>t, n)
		solve();
	return 0;
}


Measured memory_bytes: 71128