#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

priority_queue<pair<long long int, pair<int, int>>, vector<pair<long long int, pair<int, int>>>, greater<pair<long long int, pair<int, int>>>>PQ;

void func(vector<vector<pair<int,int>>>&edge,vector<vector<long long int>>&dis) {
	while (!PQ.empty()) {
		long long int c = PQ.top().first;
		int cn, cd;
		tie(cn, cd) = PQ.top().second;
		PQ.pop();
		for (auto i : edge[cn]) {
			if (i.second == -1) {
				if (cd == 100)continue;
				if (dis[i.first][cd + 1] > c) {
					dis[i.first][cd + 1] = c;
					PQ.push({ c,{ i.first,cd + 1 } });
				}
			}
			else {
				if (dis[i.first][cd + 0] > c+i.second) {
					dis[i.first][cd + 0] = c+i.second;
					PQ.push({ c+i.second,{ i.first,cd + 0 } });
				}
			}
		}
	}
	return;
}

void func2(vector<long long int>&dis, vector<pair<long long int, int>>&box) {
	long long int st = 0;
	int id = 0;
	for (int i = 1; i <= 100; i++) {
		if (dis[i] < dis[id])id = i;
	}
	box.push_back({ 0,id });
	while (1) {
	//	cout << box.back().first << " " << box.back().second << endl;
		long long int l = st, r = 1e13;
		while (r - l > 1) {
			long long int mid = (r + l) / 2;
			bool flag = false;
			for (int i = 0; i < box.back().second; i++) {
			//	cout << r << " " << i << " " << box.back().second << " " << dis[i] << " " << dis[box.back().second] << endl;
				if (mid*i + dis[i] < mid*box.back().second + dis[box.back().second]) {
					flag = true;
				}
			}
			if (flag)r = mid;
			else l = mid;
		}
//		cout << "r " << r << endl;
		if (r > 1e12) {
			break;
		}
		else {
			id = 0;
			for (int i = 1; i < box.back().second; i++) {
				if (r*i + dis[i] < r*id + dis[id]) {
					id = i;
				}
			}
			box.push_back({ r,id });
		}
	}
	return;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> N >> M >> L >> R >> K, N) {
		L--, R--, K--;
		vector<vector<long long int>>adis(N, vector<long long int>(101, MOD*MOD));
		vector<vector<long long int>>bdis(N, vector<long long int>(101, MOD*MOD));
		adis[L][0] = 0;
		bdis[R][0] = 0;
		vector<vector<pair<int, int>>>edge(N);
		for (int i = 0; i < M; i++) {
			int a, b;
			cin >> a >> b;
			a--, b--;
			string s;
			cin >> s;
			if (s == "x") {
				edge[a].push_back({ b,-1 });
				edge[b].push_back({ a,-1 });
			}
			else {
				edge[a].push_back({ b,stoi(s) });
				edge[b].push_back({ a,stoi(s) });
			}
		}
		PQ.push({ 0,{L,0} });
		func(edge, adis);
		PQ.push({ 0,{ R,0 } });
		func(edge, bdis);
		vector<pair<long long int, int>>abox;
		vector<pair<long long int, int>>bbox;
		long long int ans = MOD * MOD;
		func2(adis[K], abox);
		func2(bdis[K], bbox);
		for (int i = 0; i < abox.size(); i++) {
			long long int al = abox[i].first;
			long long int ar;
			if (i + 1 < abox.size())ar = abox[i + 1].first;
			else ar = 1e13;
			int anum = abox[i].second;
			for (int j = 0; j < bbox.size(); j++) {
			//	cout << "a b " << i << " " << j << endl;
			//	cout << abox.size() << " " << bbox.size() << endl;
				long long int bl = bbox[j].first;
				long long int br;
				if (j + 1 < bbox.size())br = bbox[j + 1].first;
				else br = 1e13;
				int bnum = bbox[j].second;
				long long int l = max(al, bl), r = min(ar, br);
				if (r <= l)continue;
				long long int st = l * anum + adis[K][anum] - (l*bnum + bdis[K][bnum]);
				long long int dif = anum - bnum;
		//		cout << "st dif " << st << " " << dif << endl;
		//		cout << l << " " << r << endl;
				if (dif == 0) {
					ans = min(ans, abs(st));
				}
				else if (st == 0) {
					ans = min(ans, abs(st));
				}
				else if (st < 0) {
					if (dif < 0) {
						ans = min(ans, abs(st));
					}
					else {
						ans = min(ans, abs(st + dif * (r - l - 1)));
						if (st + dif * (r - l) >= dif) {
							ans = min(ans, (st + dif * (r - l-1)) % dif);
						}
						if (st + dif * (r - l) > 0) {
							ans = min(ans, abs(st) % dif);
						}
					}
				}
				else {
					if (dif < 0) {
						ans = min(ans, abs(st + dif * (r - l - 1)));
						st *= -1, dif *= -1;
						if (st + dif * (r - l) >= dif) {
							ans = min(ans, (st + dif * (r - l-1)) % dif);
						}
						if (st + dif * (r - l) > 0) {
							ans = min(ans, abs(st) % dif);
						}
					}
					else {
						ans = min(ans, abs(st));
					}
				}
		//		cout << ans << endl;
			}
		}
		cout << ans << endl;
	}
}


Measured memory_bytes: 5544