#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cstring>

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef pair<int,int> II;
typedef long long LL;
#define SZ(a) int((a).size()) 
#define PB push_back 
#define ALL(c) (c).begin(),(c).end() 

struct point{
	int x,y;
	point(int x,int y):x(x),y(y){}
	bool operator<(const point &o) const {return make_pair(x,y)<make_pair(o.x,o.y);}
};

bool ycomp(const pair<point,int> &p1, const pair<point,int> &p2){
	return p1.first.y>p2.first.y;
}

struct el{
	int v,w;
	el(int v,int w):v(v),w(w){}
};

int n;
vector<II> polcok;
vector<vector<el> > g; //2*n a celvonal, 2*n+1 a start pont
int start_id, cel_id;
map<int,int> m;

void new_edge(int u, int x, int y){ //az x,y pontbol hova fog esni (az u sorszamu polcrol indulva)
	auto it=m.find(y);
	if(it!=m.begin()){
		it--; //a lefele lepve legkozelebbi polcra lepunk
		int id2=it->second; //annak a polcnak az id-je, amire raesik
		g[u].PB(el(2*id2, x-polcok[id2].first)); //bal vegehez eljutas ele
		g[u].PB(el(2*id2+1, polcok[id2].second-x)); //jobb vegehez eljutas ele
	}else //leesik a padlora
		g[u].PB(el(cel_id,0));
}

const int inf=1<<30;

int main(){
	scanf("%d",&n);
	int sx,sy; scanf("%d%d",&sx,&sy);
	vector<pair<point,int> > pend; //polcvegpontok
	int j=0;
	for(int i=0; i<n; i++){
		int y,x1,x2; scanf("%d%d%d",&y,&x1,&x2);
		if(y<=sy){ //csak azokat taroljuk el, amelyek lejjebb, vagy azonos szinten vannak a robottal
			pend.PB(make_pair(point(x1,y),2*j));
			pend.PB(make_pair(point(x2,y),2*j+1));
			polcok.PB(make_pair(min(x1,x2), max(x1,x2)));
			j++;
		}
	}
	n=j;
	start_id=2*n+1;
	cel_id=2*n;
	pend.PB(make_pair(point(sx,sy+1), start_id)); //kezdopont; a +1 azert kell, mert ha van akadaly sy magassagban, akkor baj lenne  (ezert volt fontos eldobalni a fentebbieket)

	sort(ALL(pend));

	g=vector<vector<el> >(2*n+2);
	for(int i=0; i<2*n+1; i++){
		int id=pend[i].second, x=pend[i].first.x, y=pend[i].first.y;

		if(id!=start_id) //nem a start pont
			if(m.find(y)==m.end()){ //nincs jelenleg ilyen magassagban polc, tehat ez polc kezdet
				m[y]=id/2;
				new_edge(id,x,y);
			}else{ //polc veg
				new_edge(id,x,y);
				m.erase(y);
			}
		else
			new_edge(start_id,x,y);
	}

	
	sort(ALL(pend), ycomp);
	vector<int> tav(2*n+2, inf);
	tav[start_id]=0;
	for(int i=0; i<2*n+1; i++){
		int u=pend[i].second;
		for(int j=0; j<SZ(g[u]); j++){
			el e=g[u][j];
			tav[e.v]=min(tav[e.v], tav[u]+e.w);
		}
	}
	
	printf("%d\n",tav[cel_id]+sy);

	//system("PAUSE");
	return 0;
}