import java.io.*;
import java.util.*;
public class Main{
public static BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter stdOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static final int N = 100010, M = 4 * N;
public static int[] h = new int[N];
public static int[] e = new int[M];
public static int[] ne = new int[M];
public static long[] w = new long[M];
public static long[] green = new long[M];
public static long[] red = new long[M];
public static long[] dist = new long[N];
public static boolean[] st = new boolean[N];
public static int n, m, s, t, index;
public static void add(int a, int b, long g, long r, long d) {
e[index] = b; ne[index] = h[a]; green[index] = g; red[index] = r; w[index] = d; h[a] = index++;
}
public static long getW(int ver, int i) {
long distance = dist[ver] % (green[i] + 2 * Math.abs(w[i]) + red[i]);
if(w[i] > 0) {
if(distance - green[i] < 0) return w[i];
else return 3 * w[i] + red[i] + green[i] - distance;
}else {
long ww = -w[i];
if(distance - green[i] < 0) return green[i] - distance + 2 * ww;
else if(distance - green[i] - ww < 0) return 2 * ww + green[i] - distance;
else if(distance - green[i] - ww - red[i] < 0) return ww;
else return red[i] + 4 * ww + 2 * green[i] - distance;
}
}
public static void main(String[] args) throws IOException{
String[] sp = stdIn.readLine().split(" ");
n = Integer.parseInt(sp[0]); m = Integer.parseInt(sp[1]); s = Integer.parseInt(sp[2]); t = Integer.parseInt(sp[3]);
Arrays.fill(h, -1); Arrays.fill(dist, Long.MAX_VALUE);
while(m-- > 0) {
sp = stdIn.readLine().split(" ");
int u = Integer.parseInt(sp[0]); int v = Integer.parseInt(sp[1]); long g = Long.parseLong(sp[2]);
long r = Long.parseLong(sp[3]); long d = Long.parseLong(sp[4]);
add(u, v, g, r, d); add(v, u, g, r, -d);
}
//堆优化版的Dijkstra
PriorityQueue<PII> q = new PriorityQueue<PII>();
dist[s] = 0;
q.add(new PII(0, s));
while(!q.isEmpty()) {
PII temp = q.poll();
int ver = temp.b;
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; i != -1; i = ne[i]) {
int j = e[i];
long wight = getW(ver, i);
if(dist[j] > dist[ver] + wight) {
dist[j] = dist[ver] + wight;
q.add(new PII(dist[j], j));
}
}
}
if(dist[t] == Long.MAX_VALUE) stdOut.println("-1");
else stdOut.println(dist[t]);
stdOut.flush();
}
}
class PII implements Comparable<PII>{
long a; int b;
public PII(long a, int b) {
this.a = a; this.b = b;
}
public int compareTo(PII o) {
return Long.compare(a, o.a);
}
}