#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 TheGridDivOne { public:
int find(vector <int> x, vector <int> y, int k)
{
enum {X, Y};
typedef tuple<int,int> Point;
set<Point> blocked;
map<int, set<int>> x2y;
x2y[0].emplace(0);
for(int i=0; i<x.size(); ++i) {
blocked.emplace(x[i], y[i]);
x2y[x[i]-1].emplace(y[i]-1);
x2y[x[i]-1].emplace(y[i]+0);
x2y[x[i]-1].emplace(y[i]+1);
x2y[x[i]+0].emplace(y[i]-1);
x2y[x[i]+0].emplace(y[i]+1);
x2y[x[i]+1].emplace(y[i]-1);
x2y[x[i]+1].emplace(y[i]+0);
x2y[x[i]+1].emplace(y[i]+1);
}
map<int, set<int>> x2y_aug = x2y;
for(auto it=x2y.begin(); it!=x2y.end(); ++it) {
auto kt = it; ++kt;
if(kt != x2y.end()) {
for(int y: it->second)
x2y_aug[kt->first].emplace(y);
}
if(it != x2y.begin()) {
auto kt = it; --kt;
for(int y: it->second)
x2y_aug[kt->first].emplace(y);
}
}
vector<Point> pts;
for(auto& xys: x2y_aug) {
int x = xys.first;
for(int y: xys.second) {
Point q(x,y);
if(!blocked.count(q))
pts.emplace_back(q);
}
}
typedef int Vert;
const int N = pts.size();
const int S = std::find(pts.begin(), pts.end(), Point(0,0)) - pts.begin();
typedef int Cost;
typedef pair<Cost,Vert> Edge;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
Graph G(N);
for(int i=0; i<N; ++i)
for(int k=i+1; k<N; ++k) {
bool connected = true;
int x1 = get<X>(pts[i]);
int x2 = get<X>(pts[k]);
int y1 = get<Y>(pts[i]);
int y2 = get<Y>(pts[k]);
for(Point b: blocked) {
if(min(y1,y2)<=get<Y>(b) && get<Y>(b)<=max(y1,y2) &&
min(x1,x2)<=get<X>(b) && get<X>(b)<=max(x1,x2)) {
connected = false;
break;
}
}
// If not blocked then add i<-->k with Manhattan cost
if(connected) {
G[i].emplace_back(abs(x1-x2)+abs(y1-y2), k);
G[k].emplace_back(abs(x1-x2)+abs(y1-y2), i);
}
}
// Dijkstra
priority_queue<Edge, vector<Edge>, greater<Edge>> Q;
Q.emplace(0, S);
vector<Cost> D(N, -1);
while(!Q.empty()) {
Cost c = Q.top().first;
Vert v = Q.top().second;
Q.pop();
if(D[v]!=-1)
continue;
D[v] = c;
for(auto e: G[v]) {
Cost c2 = c + e.first;
Vert q = e.second;
if(D[q]==-1)
Q.emplace(c2, q);
}
}
int best = 0;
for(Vert v=0; v<N; ++v) {
if(D[v] != -1 && D[v]<=k) {
// the point
best = max(best, get<X>(pts[v]));
// if not blocked...
Point right(get<X>(pts[v])+1, get<Y>(pts[v]));
if(blocked.count(right))
continue;
// go more east
bool skip = false;
for(Edge e: G[v]) {
Vert u = e.second;
if(get<Y>(pts[u]) == get<Y>(pts[v]) && get<X>(pts[u]) > get<X>(pts[v])
&& D[u]!=-1 && D[u]<=k) {
skip = true;
}
}
if(!skip) {
int sec = k-D[v];
best = max(best, get<X>(pts[v]) + sec);
}
}
}
return best;
}
};
// 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 int& Expected, const int& 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(_, TheGridDivOne().find(x, y, k));}
int main(){
CASE(0)
int x_[] = {1,1,1,1};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {-2,-1,0,1};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int k = 4;
int _ = 2;
END
CASE(1)
int x_[] = {-1, 0, 0, 1};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {0, -1, 1, 0};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int k = 9;
int _ = 0;
END
CASE(2)
vector <int> x;
vector <int> y;
int k = 1000;
int _ = 1000;
END
CASE(3)
int x_[] = {1,0,0,-1,-1,-2,-2,-3,-3,-4,-4};
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = {0,-1,1,-2,2,-3,3,-4,4,-5,5};
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int k = 47;
int _ = 31;
END
/*
CASE(4)
int x_[] = ;
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = ;
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int k = ;
int _ = ;
END
CASE(5)
int x_[] = ;
vector <int> x(x_, x_+sizeof(x_)/sizeof(*x_));
int y_[] = ;
vector <int> y(y_, y_+sizeof(y_)/sizeof(*y_));
int k = ;
int _ = ;
END
*/
}
// END CUT HERE