#include<bits/stdc++.h> 
using namespace std;

int main(){
  int64_t n,m,x,y;
  cin>>n>>m>>x>>y;
  x--,y--;
  vector<vector<vector<int64_t>>>trn(n);
  for(int64_t i=0,a,b,c,d;i<m;i++){
    cin>>a>>b>>c>>d;
    a--,b--;
    trn[a].push_back(vector<int64_t>{b,c,d});
    trn[b].push_back(vector<int64_t>{a,c,d});
  }
  priority_queue<pair<int64_t,int64_t>,vector<pair<int64_t,int64_t>>,greater<pair<int64_t,int64_t>>>dijk;
  vector<int64_t>dist(n,9e18);
  dist[x]=0;
  dijk.push({(int64_t)0,x});
  while(!dijk.empty()){
    auto i=dijk.top();
    dijk.pop();
    if(i.first>dist[i.second])continue;
    for(auto j:trn[i.second]){
      int64_t e=j[2];
      int64_t d=((dist[i.second]+e-1)/e+0)*e+j[1];
      if(dist[j[0]]>d){
        dist[j[0]]=d;
        dijk.push({d,j[0]});
      }
    }
  }
  cout<<(dist[y]==9e18?(int64_t)-1:dist[y])<<'\n';
}
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
//debug 
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

int solve(){
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int x = c - a;
  int y = d - b;
  //0
  if(x == 0 && y == 0) return 0;
  //1
  if( x - y == 0 || x + y == 0) return 1;
  if(abs(x)+abs(y) <= 3) return 1;
  //2
  //斜め移動 2
  if((x+y)%2 == 0) return 2;
  //斜め１，縦横1
  for(int i =  -3; i <= 3; i++){
    for(int j = -3; j <= 3; j++){
      if(abs(i)+abs(j) > 3) continue;
      if((x+i) - (y+j) == 0 || (x+i) + (y+j) == 0 ) return 2;
    }
  }
  //縦横2
  if(abs(x)+abs(y) <= 3*2) return 2;
  //3: 0,1,2以外なら3手
  return 3;
}

int main(){
  cout << solve() << endl;
  return 0;
}