// https://szkopul.edu.pl/problemset/problem/vE94xK5yr-lSfcAMD3IKFd7F/site/?key=statement
// Gwiazdy
#include <bits/stdc++.h>
using namespace std;
#define int long long
constexpr int sizik = 1000 * 1001;
#define ar std::array
#define pr std::pair
#define vec std::vector
#define PRINT_ANS
typedef vec<vec<int>> _kra;
int n, s;
ar<int, sizik> l, r, visited;
constexpr int LEWO = 1, PRAWO = 2;
// get direction for i-th operation
int get_dir(int i) {
int firstDir = LEWO;
if (r[i] < l[i]) firstDir = PRAWO;
return firstDir;
}
int num_of_operations_in_good_direction(int start) {
int udir = get_dir(start);
int x = 1;
for (int i = start + 1; i <= n - 1; i++, x++) {
if (get_dir(i) != udir) {
break;
}
}
return x;
}
void solve() {
std::cin >> n >> s;
for (int i = 1; i <= n - 1; i++) {
std::cin >> l[i] >> r[i];
}
visited[s] = true;
int firstDir = get_dir(1);
int na_lewo = s - 1, na_prawo = n - s;
int firstly = num_of_operations_in_good_direction(1);
bool isGood = true;
int OPERAEFOWAION = -1;
if (firstDir == LEWO) {
if (firstly > na_lewo) {
OPERAEFOWAION = na_lewo + 1;
isGood = false;
}
} else {
if (firstly > na_prawo) {
OPERAEFOWAION = na_prawo + 1;
isGood = false;
}
}
std::deque<int> d;
int new_starting_operation = 1;
int ans = 0;
std::vector<int> path_ans{s};
if (!isGood) {
int q_min = INT64_MAX, j = 1;
for (int i = 1; i <= OPERAEFOWAION; i++) {
int q_curr = abs(l[i] - r[i]);
if (q_curr < q_min) {
q_min = q_curr;
j = i;
}
}
new_starting_operation = j + 1;
int curr = n;
std::vector<int> dop;
if (firstDir == LEWO) curr = 1;
for (int i = 1; i <= j - 1; i++) {
while (visited[curr]) {
if (firstDir == LEWO) {
curr++;
} else {
curr--;
}
}
visited[curr] = true;
dop.push_back(curr);
}
std::reverse(dop.begin(), dop.end());
for (const auto& a : dop) {
path_ans.push_back(a);
}
int t = get_dir(j + 1);
if (j == n - 1) {
t = LEWO;
}
if (t == LEWO) {
int curr_x = n;
while (visited[curr_x])
curr_x--;
path_ans.push_back(curr_x);
visited[curr_x] = true;
} else {
int curr_x = 1;
while (visited[curr_x])
curr_x++;
path_ans.push_back(curr_x);
visited[curr_x] = true;
}
} else {
}
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
d.push_back(i);
}
}
for (int i = new_starting_operation; i <= n - 1;) {
int ile = num_of_operations_in_good_direction(i);
int gdzie = get_dir(i);
std::vector<int> dododania;
if (gdzie == LEWO) {
for (int j = 0; j < ile; j++) {
dododania.push_back(d.front());
d.pop_front();
}
} else {
for (int j = 0; j < ile; j++) {
dododania.push_back(d.back());
d.pop_back();
}
}
std::reverse(dododania.begin(), dododania.end());
for (const auto& a : dododania) {
path_ans.push_back(a);
}
i += ile;
}
if ((int)path_ans.size() != n) {
std::cout << "WTF MAN\n";
}
for (int i = 1; i < (int)path_ans.size(); i++) {
if (path_ans[i] < path_ans[i - 1]) {
// w lewo!
ans += l[i];
} else {
ans += r[i];
}
}
std::cout << ans << '\n';
#ifdef PRINT_ANS
for (const auto& a : path_ans) {
std::cout << a << " ";
}
std::cout << '\n';
#endif
}
int32_t main() {
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
int t = 1;
// std::cin >> t;
for (; t > 0; t--) {
solve();
}
return 0;
}