#include<bits/stdc++.h>
#include<bits/extc++.h>
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(nullptr);std::cout.tie(nullptr);
#define all(x) (x).begin(),(x).end()
#define Yes(x,y) cout<<((x)?"Yes":"No")<<y
#define YES(x,y) cout<<((x)?"YES":"NO")<<y
#define ls ((u)<<1)
#define rs ((u)<<1|1)
#define mid (((l)+(r))>>1)
#define lowbit(x) ((x)&(-(x)))
#define itn int
#define asn ans
#define reisze resize
#define pdd pair<double,double>
#define pll pair<LL,LL>
#define pii pair<int,int>
#define tll tuple<LL,LL,LL>
#define tii tuple<int,int,int>
#define plll pair<LLL,LLL>
#define ULL unsigned long long
#define LL long long
#define LLL __int128
#define ld long double
#define ui64 uint64_t
#define ui32 uint32_t
using namespace std;
template<typename T>
T fang(const T& a){
return a*a;
}
template<typename T,typename Q>
bool chmax(T& u1,T& u2,const Q& v){
if(v>u1) { u2 = u1, u1 = v;return true;}
if(v>u2){u2=v;return true;}
return false;
}
template<typename T,typename Q>
bool chmin(T& u1,T& u2,const Q& v){
if(v<u1) { u2 = u1, u1 = v;return true;}
if(v<u2){u2=v;return true;}
return false;
}
template<typename T,typename Q>
bool chmin(T& a,const Q& b){
return a > b && (a = b, true);
}
template<typename T,typename Q>
bool chmax(T& a,const Q& b){
return a<b&&(a=b,true);
}
template<typename t1,typename t2>
istream& operator>>(istream& in,pair<t1,t2>& pa){
in>>pa.first>>pa.second;
return in;
}
template<typename t1,typename t2>
ostream& operator<<(ostream& out,const pair<t1,t2>& pa){
out<<pa.first<<' '<<pa.second;
return out;
}
template<typename T>
istream& operator>>(istream& in,vector<T>& arr){
for(auto& v:arr)in>>v;
return in;
}
template<typename T>
ostream& operator<<(ostream& out,const vector<T>& arr){
for(auto& v:arr)out<<v<<' ';
return out;
}
const ld eps=1e-9;
const int N=6e5+5;
const int SIZ=1e7;
const LL inf=1e17;
struct P{
int l,r;
LL h;
bool operator<(const P& o)const{
return h<o.h;
}
bool operator==(const P& o)const{
return l==o.l&&r==o.r&&h==o.h;
}
};
int tag[N*4];
void change(int u,int l,int r,int L,int R,int c){
if(L<=l&&R>=r){
tag[u]=c;
return;
}
if(L<=mid)change(ls,l,mid,L,R,c);
if(R>mid)change(rs,mid+1,r,L,R,c);
}
int query(int u,int l,int r,int p){
if(tag[u])return tag[u];
if(l==r)return 0;
return p<=mid?query(ls,l,mid,p):query(rs,mid+1,r,p);
}
int R=1e5;
struct E{
int y;
LL val;
bool operator<(const E& o)const{
return val>o.val;
}
};
signed main(){
IOS;
int n;
int s,t;
cin>>n>>s>>t;
vector<P> A(n+1);
for(int i=1;i<=n;i++){
auto& p=A[i];
cin>>p.l>>p.r>>p.h;
}
P p0=A[s];
P p1=A[t];
sort(all(A));
for(int i=1;i<=n;i++){
if(A[i]==p0)s=i;
if(A[i]==p1)t=i;
}
vector<vector<E>> G(2*n+2,vector<E>());
for(int i=1;i<=n;i++){
auto p=A[i];
if(i==t){
change(1,1,R,p.l,p.r,i);
continue;
}
int pr= query(1,1,R,p.r);
int pl=query(1,1,R,p.l);
if(pl&&pl!=t){
LL dl=abs(p.l-A[pl].l);
LL dr=abs(p.l-A[pl].r);
LL dis=p.h-A[pl].h;
G[i*2].emplace_back(pl*2,dl+dis);
G[i*2].emplace_back(pl*2+1,dr+dis);
}
if(pr&&pr!=t){
LL dl=abs(p.r-A[pr].l);
LL dr=abs(p.r-A[pr].r);
LL dis=p.h-A[pr].h;
G[i*2+1].emplace_back(pr*2,dl+dis);
G[i*2+1].emplace_back(pr*2+1,dis+dr);
}
if(pl==t){
LL h=p.h-A[pl].h;
G[i*2].emplace_back(1,h);
}
if(pr==t){
LL h=p.h-A[pr].h;
G[i*2+1].emplace_back(1,h);
}
change(1,1,R,p.l,p.r,i);
}
vector<LL> dis(2*n+2,inf);
for(int i=1;i<=n;i++){
if(i==t)continue;
G[i*2].emplace_back(i*2+1,A[i].r-A[i].l);
G[i*2+1].emplace_back(i*2,A[i].r-A[i].l);
}
priority_queue<E> q;
q.push({s*2,0});
while(!q.empty()){
auto [x,val]=q.top();q.pop();
if(dis[x]<=val)continue;
dis[x]=val;
for(auto [ne,ad]:G[x]){
if(dis[ne]>dis[x]+ad){
q.emplace(ne,ad+dis[x]);
}
}
}
if(dis[1]==inf){
cout<<-1;
}else cout<<dis[1];
}