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

int n, s, x, p;
deque<pair<int, long long> > d;

long long amx(int a) {
	if(s >= d.back().first) {
		return s - d.front().first;
	} else if(s <= d.front().first) {
		return d.back().first - s;
	}
	int na;
	long long D = d.back().first - d.front().first;
	if(d.front().second >= d.back().second) {
		pair<int, long long> t = d.front();
		t.second += d.back().second;
		d.pop_back();
		d.pop_front();
		d.push_front(t);
		na = 0;
	} else {
		pair<int, long long> t = d.back();
		t.second += d.front().second;
		d.pop_front();
		d.pop_back();
		d.push_back(t);
		na = 1;
	}
	return amx(na) + (long long)(a != na) * D;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> s;
	for(int i = 1; i <= n; ++i) {
		cin >> x >> p;
		d.push_back({x, p});
	}
	cout << amx(2) << endl;
	return 0;
}

#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int main() {
	int N, M;
	cin >> N >> M;
	vector<vector<int>> a(N, vector<int>(M));
	for (int i = 0; i < N; ++i) {
		for (int j = 0; j < M; ++j) {
			cin >> a[i][j]; --a[i][j];
		}
	}

	int ans = -1;
	for (int n = 1; n <= N; ++n) {
		vector<bool> sports(M, true);
		vector<int> join(N, 0);
		while (true) {
			bool ok = true;
			vector<int> cnt(M, 0);
			for (int i = 0; i < N; ++i) {
				while (join[i] <= M) {
					if (join[i] == M) {
						ok = false;
						break;
					}
					if (sports[ a[i][join[i]] ]) {
						++cnt[ a[i][join[i]] ];
						break;
					}
					++join[i];
				}
			}
			if (!ok) { break; }
			for (int i = 0; i < M; ++i) {
				if (cnt[i] > n) {
					ok = false;
					sports[i] = false;
				}
			}
			if (ok) {
				ans = n;
				break;
			}
		}
		if (ans > 0) { break; }
	}
	cout << ans << endl;

	return 0;
}
