// Author:   old_school
// Created:  20.02.2021 17:30:29

#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma") 
#pragma GCC optimization ("unroll-loops")
using namespace std;
#define lld               long long int
#define ld                long double
#define pb                push_back
#define ppb               pop_back 
#define mp                make_pair
#define F                 first
#define S                 second
#define nl                '\n'
#define all(c)            (c).begin(),(c).end()
#define rall(c)           (c).rbegin(),(c).rend()
#define sz(c)             (c).size()
#define tr(x,a)           for(auto &a : x)
#define psnt(x,a)         (x).find(a)!=(x).end()
#define vpsnt(x,a)        find(all(x),a)!=(x).end()
#define MOD               1000000007
#define tod               100005
#define pi                3.1415926536
#define itr(i,a,b)        for(lld i=a;i<=b;i++)
#define itrn(i,a,b)       for(lld i=a;i>=b;i--)
#define iot(n)            for(lld i=0;i<n;i++)
#define pls(n,arr)        lld arr[n]; iot(n)  cin>>arr[i];
#define bye               fflush(stdout)

typedef pair<lld,lld>     pii;
typedef pair<string,lld>  psi;

template <typename T>
bool mycomp(T x,T y){ 
   return (x==y);  //give your condition here
}
bool paircomp(const pair<lld,lld> &x,const pair<lld,lld> &y){ 
return x.second<y.second;
}
vector<pair<lld,pair<lld,lld> > > adj[tod];
void dijkstra(lld V,lld source,lld dist[]){
    priority_queue<pair<lld,lld> ,vector<pair<lld,lld> >,greater<pair<lld,lld> > > pq; //minheap
    for(lld i=0;i<=V;i++)
       dist[i]=-1;
    dist[source]=0;
    pq.push(make_pair(dist[source],source));
    while(!pq.empty()){
        lld u=pq.top().second;
        pq.pop();
        for(auto &p:adj[u]){
            lld v=p.first,weight=p.second.first,lx=p.second.second;
            lld z=dist[u]/lx;
               lld ex=0;
               if(dist[u]%lx){
               	 z++;
               }
               z*=lx;
               ex=z-dist[u];   
            if (dist[v]==-1) 
            {   
                dist[v] = dist[u] + ex+weight; 
                pq.push(make_pair(dist[v], v)); 

            }
            else if(dist[v]>dist[u]+ex+weight){
                dist[v]=dist[u]+ex+weight;
                pq.push(make_pair(dist[v], v));   
            }
        }
    }
}
void solve(){
   lld n,m,x,y; cin>>n>>m>>x>>y;
   iot(m){
   	lld a,b,t,k; cin>>a>>b>>t>>k;
   	adj[a].pb({b,{t,k}});
   	adj[b].pb({a,{t,k}});
   }
   lld dist[n+1];
   dijkstra(n,x,dist);
   cout<<dist[y];
}
int main(){
       ios_base::sync_with_stdio(false);
             cin.tie(0);
            cout.tie(0);
      lld t=1;// cin>>t;  
      while(t--){
            solve();
            cout<<endl;
      }
}


#pragma GCC optimize("Ofast", "unroll-loops")
#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

#define ll long long

using pos = pair<int, int>;

bool canMoveWith1Move(const pos& from, const pos& to){
    return (
        (from.first + from.second == to.first + to.second) ||
        (from.first - from.second == to.first - to.second) ||
        (abs(from.first - to.first) + abs(from.second - to.second) <= 3)
    );
}

bool canMoveWith2Moves(const pos& from, const pos& to){
    auto color = [](const pos& p) -> int {
        return ((p.first + p.second) % 2 + 2) % 2;
    };

    bool flg = false;
    for (int dx = -3; dx <= 3; ++dx){
        for (int dy = -3; dy <= 3; ++dy){
            pos tmp = {from.first + dx, from.second + dy};
            flg |= (canMoveWith1Move(from, tmp) && canMoveWith1Move(tmp, to));
        }
    }

    return (
        (color(from) == color(to)) ||
        (abs(from.first + from.second - to.first - to.second) <= 4) ||
        (abs(from.first - from.second - to.first + to.second) <= 4) ||
        (flg)
    );
}

int solve(const pos& from, const pos& to){
    if (from == to) return 0;
    if (canMoveWith1Move(from, to)) return 1;
    if (canMoveWith2Moves(from, to)) return 2;
    return 3;
}

int main(void){
    int x, y;
    cin >> x >> y;
    pos from = {x, y};
    cin >> x >> y;
    pos to = {x, y};
    cout << solve(from, to) << endl;
    return 0;
}