#include<bits/stdc++.h>
using namespace std;
#define rep(x,y,z) for(int (x)=(y);(x)<(z);i++)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
const int maxn=1e4+10;
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
int n,m;
int x,y,z,p1,q1,s,t;
pair<int,int> p[maxn];
vector<piii> g[maxn];
vector<int> rt1,rt2;
int nxt1[maxn],nxt2[maxn];
void solve1(int x){
if(x==-1) return ;
solve1(nxt1[x]);
rt1.push_back(x);
}
void solve2(int x){
if(x==-1) return ;
solve2(nxt2[x]);
rt2.push_back(x);
}
pii gao(pii x,pii y){
return mk(x.fi+y.fi,x.se+y.se);
}
struct node{
int x;
bool operator<(const node& y) const {
return p[x]>p[y.x];
}
};
void bfs(int s,int flag){
p[s]=mk(0,0);
priority_queue<node> pq;pq.push(node{s});
while(!pq.empty()){
node tp=pq.top();pq.pop();
int x=tp.x;
if(x==t) return ;
rep(i,0,g[x].size()){
int v=g[x][i].fi;
pii tmp;
if(flag) tmp=gao(p[x],g[x][i].se);
else tmp=gao(p[x],mk(g[x][i].se.se,1));
if(p[v]>tmp){
p[v]=tmp;
if(flag) nxt1[v]=x;
else nxt2[v]=x;
pq.push(node{v});
}
}
}
return ;
}
pair<int,int> tp;
int main(){
mst(nxt1,-1),mst(nxt2,-1);
cin>>n>>m;
rep(i,0,m){
cin>>x>>y>>z>>p1>>q1;
g[x].push_back(mk(y,mk(p1,q1)));
if(!z) g[y].push_back(mk(x,mk(p1,q1)));
}
cin>>s>>t;
rep(i,0,maxn) p[i]=mk(maxn*maxn,maxn*maxn);
bfs(s,1);solve1(t);
tp=p[t];
rep(i,0,maxn) p[i]=mk(maxn*maxn,maxn*maxn);
bfs(s,0);solve2(t);
if(rt1!=rt2){
printf("Distance = %d: ",tp.fi);
rep(i,0,rt1.size()){
cout<<rt1[i];
if(i!=rt1.size()-1) cout<<" -> ";
else cout<<"\n";
}
printf("Time = %d: ",p[t].fi);
rep(i,0,rt2.size()){
cout<<rt2[i];
if(i!=rt2.size()-1) cout<<" -> ";
else cout<<"\n";
}
}else{
printf("Distance = %d; Time = %d: ",tp.fi,p[t].fi);
rep(i,0,rt1.size()){
cout<<rt1[i];
if(i!=rt1.size()-1) cout<<" -> ";
else cout<<"\n";
}
}
return 0;
}