1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;
typedef long long int ll;

class prz
{
public:
ll S,L,R,N;

	prz();
	prz(const ll n,const ll s,const ll l,const ll r);
	prz(const prz&);
prz operator=(const prz&);
};

prz::prz()
{
	N = S = L = R = 0;
}

prz::prz(const ll n,const ll s,const ll l,const ll r)
{
	N = n;
	S = s;
	L = l;
	R = r;
}

prz::prz(const prz &s)
{
	N = s.N;
	S = s.S;
	L = s.L;
	R = s.R;
}

prz prz::operator=(const prz &s)
{
	N = s.N;
	S = s.S;
	L = s.L;
	R = s.R;

	return *this;
}

bool comp(prz a, prz b)
{
	if (a.L == b.L) return a.N < b.N;
	else return a.L < b.L;
}

typedef vector<prz> vP;

////////////////////////////////////////////////////

int main()
{
ll N,M,S,ms,m,l,r,f;
prz tmp;
vP L,R,V;

	f = 0;
	scanf("%lld %lld %lld",&N,&M,&S);
	for (m = 0; m < M; m++)
	{
		scanf("%lld %lld",&l,&r);
		if (S >= l && S <= r) f = 1;
		V.push_back(prz(m,S,l,r));
	}
	if (f == 0) V.push_back(prz(m,S,S,S));

//	printf("%lld %lld %lld %lld\n",N,M,S,f);
	
	sort(V.begin(),V.end(),comp);

	ms = 0;
	while (m < M && V[ms].L > S || V[ms].R < S) ms++;
/*
	for (vP::iterator i = V.begin(); i != V.end(); ++i)
	{
		printf("%lld %lld\n",i->L,i->R);
	}

	printf("%lld %lld %lld\n",ms,V[ms].L,V[ms].R);
*/
	m = ms;
	while (m > 0 && V[m].L - 1 == V[m-1].R) m--;
	l = V[m].L - 1;

	m = ms;
	while (m < N && V[m].R + 1 == V[m+1].L) m++;
	r = V[m].R + 1;

//	printf("%lld %lld\n",l,r);

	if (l <= 0) printf("%lld\n",r);
	else if (r >= N) printf("%lld\n",l);
	else if (S - l < r - S) printf("%lld\n",l);
	else if (S - l > r - S) printf("%lld\n",r);
	else if (l < r) printf("%lld\n",l);
	else printf("%lld\n",r);


return 0;
}