#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
class TwoLadders { public:
long long minSteps(int sx, int lx1, int lx2, vector <int> X, vector <int> Y)
{
map<int, vector<int>> y2x;
for (int i = 0; i < X.size(); ++i)
y2x[Y[i]].push_back(X[i]);
map<int, pair<int, int>> y2mM;
for (auto kv : y2x)
y2mM[kv.first] = make_pair(
*min_element(kv.second.begin(), kv.second.end()),
*max_element(kv.second.begin(), kv.second.end())
);
auto cur = y2mM.begin();
// best 2 move candidates in the prvious level
int p1 = sx;
int p2 = sx;
LL b1 = 0;
LL b2 = 0;
int yy = 0;
if(cur->first == 0) {
int m = cur->second.first;
int M = cur->second.second;
++cur;
p1 = m;
b1 = abs(sx - M) + abs(M - m);
p2 = M;
b2 = abs(sx - m) + abs(M - m);
}
for (; cur != y2mM.end(); ++cur) {
int ydiff = cur->first - yy;
yy = cur->first;
int m = cur->second.first;
int M = cur->second.second;
int prev_p1 = p1;
int prev_p2 = p2;
LL prev_b1 = b1;
LL prev_b2 = b2;
p1 = m;
b1 = min({
prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - M) + abs(M - m),
prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - M) + abs(M - m),
prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - M) + abs(M - m),
prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - M) + abs(M - m),
});
p2 = M;
b2 = min({
prev_b1 + ydiff + abs(prev_p1 - lx1) + abs(lx1 - m) + abs(M - m),
prev_b1 + ydiff + abs(prev_p1 - lx2) + abs(lx2 - m) + abs(M - m),
prev_b2 + ydiff + abs(prev_p2 - lx1) + abs(lx1 - m) + abs(M - m),
prev_b2 + ydiff + abs(prev_p2 - lx2) + abs(lx2 - m) + abs(M - m),
});
}
return min(b1, b2);
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, TwoLadders().minSteps(sx, lx1, lx2, X, Y));}
int main(){
CASE(0)
int sx = 10;
int lx1 = 0;
int lx2 = 100;
int X_[] = {47, 42};
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = {0, 0};
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = 37LL;
END
CASE(1)
int sx = 10;
int lx1 = 8;
int lx2 = 11;
int X_[] = {10, 12};
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = {1, 1};
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = 5LL;
END
CASE(2)
int sx = 10;
int lx1 = 8;
int lx2 = 42;
int X_[] = {10, 12};
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = {1, 1};
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = 7LL;
END
CASE(3)
int sx = 10;
int lx1 = 8;
int lx2 = 42;
int X_[] = {10, 100, 12};
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = {1, 0, 1};
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = 181LL;
END
CASE(4)
int sx = 500000000;
int lx1 = 1;
int lx2 = 999999999;
int X_[] = {0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000, 0, 1000000000};
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = {1, 1, 2, 2, 3, 3, 4, 4, 7, 7, 999999947, 999999947, 900000047, 900000047};
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = 8499999959LL;
END
/*
CASE(5)
int sx = ;
int lx1 = ;
int lx2 = ;
int X_[] = ;
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = ;
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = LL;
END
CASE(6)
int sx = ;
int lx1 = ;
int lx2 = ;
int X_[] = ;
vector <int> X(X_, X_+sizeof(X_)/sizeof(*X_));
int Y_[] = ;
vector <int> Y(Y_, Y_+sizeof(Y_)/sizeof(*Y_));
long long _ = LL;
END
*/
}
// END CUT HERE