#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cassert>
#include <chrono>

using namespace std;

#define INF INT_MAX
typedef long long ullong;

struct interval
{
	int from, to;
	interval() {}
	interval(int _from, int _to):from(_from), to(_to) {}
};

struct line
{
	interval x;
	int y;
	const bool operator<(const line& rhs) const
	{
		return y==rhs.y?x.from<rhs.x.from:y<rhs.y;
	}
};

struct segment
{
	static const int offset = 1<<21, N = offset<<1;
	int h[N];
	bool flag[N];
	segment(){}
	void update(int val, int from, int to, int node=1, int node_range_from = 0, int node_range_to = offset-1)
	{
		if(from>to) return;
		if(node_range_from>to || node_range_to<from) return;
		if(from <= node_range_from && node_range_to <= to)
		{
			h[node] = val;
			if(node_range_from!=node_range_to)
			{
				h[2*node] = val; flag[2*node] = true;
				h[2*node+1] = val; flag[2*node + 1] = true;
			}
		}
		else
		{
			update(val, from, to, 2*node, node_range_from, (node_range_from+node_range_to)/2);
			update(val, from, to, 2*node+1, (node_range_from+node_range_to)/2+1, node_range_to);
		}
	}
	int query(int x, int node = 1, int node_range_from = 0, int node_range_to = offset - 1)
	{
		if(x<node_range_from || x>node_range_to) return -1;
		if(node_range_from == node_range_to)
		{
			flag[node] = false;
			return h[node];
		}
		else
		{
			if(flag[node])
			{
				flag[node] = false;
				h[2*node] = h[node]; flag[2*node] = true;
				h[2*node+1] = h[node]; flag[2*node + 1] = true;
			}
			if(x<=(node_range_from+node_range_to)/2)
			{
				return query(x, 2*node, node_range_from, (node_range_from+node_range_to)/2);
			}
			else
			{
				return query(x, 2*node +1, (node_range_from+node_range_to)/2 +1, node_range_to);
			}
		}
	}
	void dump(int node=1, int depth = 0, int node_range_from = 0, int node_range_to = offset-1)
	{
		if(node>=N) return;
		dump(2*node, depth+1, node_range_from, (node_range_from+node_range_to)/2);
		for(int i=0;i<depth;i++) printf("  ");
		printf("%3d [%3d,%3d]\n", h[node], node_range_from, node_range_to);
		dump(2*node+1, depth+1, (node_range_from+node_range_to)/2+1, node_range_to);
	}
} tree;

const int N=200008;

int n, start_x, start_y;
line a[N];
ullong d[N][2];


int main()
{
	//chrono::high_resolution_clock::time_point start_time = chrono::high_resolution_clock::now();
	scanf("%d%d%d", &n, &start_x, &start_y);
	a[0].y = 0; a[0].x.from = 0; a[0].x.to = 1000000;
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d%d", &a[i].y, &a[i].x.from, &a[i].x.to);
		if(a[i].x.from>a[i].x.to) swap(a[i].x.to, a[i].x.from);
	}
	sort(a, a+n+1);

	d[0][0] = 0; d[0][1] = 0;
	for(int i=1;i<=n && a[i].y<=start_y;i++)
	{
		int idx0 = tree.query(a[i].x.from), idx1 = tree.query(a[i].x.to);

		if(a[idx0].y == a[i].y)
		{
			while(a[idx0].y == a[i].y) 
			{
				a[i].x.from = a[idx0].x.from;
				idx0 = tree.query(a[idx0].x.from);
			}
		}
		if(a[idx1].y == a[i].y) 
		{
			while(a[idx1].y == a[i].y) 
			{
				a[i].x.to = a[idx1].x.to;
				idx1 = tree.query(a[idx1].x.to);
			}
		}
		if(idx0)
		{
			d[i][0] = a[i].y-a[idx0].y+min(a[i].x.from-a[idx0].x.from + d[idx0][0], a[idx0].x.to-a[i].x.from + d[idx0][1]);
		}
		else
		{
			d[i][0] = a[i].y;
		}
		if(idx1)
		{
			d[i][1] = a[i].y-a[idx1].y+min(a[i].x.to-a[idx1].x.from + d[idx1][0], a[idx1].x.to-a[i].x.to + d[idx1][1]);
		}
		else
		{
			d[i][1] = a[i].y;
		}
		tree.update(i, a[i].x.from+1, a[i].x.to-1);
	}

	int idx = tree.query(start_x);
	if(idx)
	{
		printf("%lld\n", start_y-a[idx].y+min(start_x - a[idx].x.from + d[idx][0], a[idx].x.to -start_x + d[idx][1]));
	}
	else
	{
		printf("%d\n", start_y);
	}

	return 0;
}