# include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define y0 asdsahhajkshdasdks
#define y1 asdjklasjldkjklqwj
#define next asdjlsadjlwjerwj
#define prev sdjfkljlkwjrweur
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const ll inf = (ll)1e9 + 7;
const ll maxn = (ll)2e5 + 7;
const ll lmaxn = (ll)6e6 + 7;
const ll linf = (ll)1e15 + 7;
const ld eps = ld(1e-11);
const ll dx[] = {-1, 0, 0, 1};
const ll dy[] = {0, -1, 1, 0};
ll gcd(ll a, ll b){
while(b) a %= b, swap(a, b);
return a | b;
}
ll lcm(ll a, ll b){
return a * b / gcd(a, b);
}
ll n, x1, y1, x2, y2;
ll x[maxn], y[maxn];
map < ll, set < ll > > xx, yy;
map < pll, ll > d;
int main(){
freopen("lasers.in", "r", stdin);
freopen("lasers.out", "w", stdout);
scanf("%lld %lld %lld %lld %lld", &n, &x1, &y1, &x2, &y2);
for(ll i = 1; i <= n; ++i){
scanf("%lld %lld", &x[i], &y[i]);
xx[x[i]].insert(y[i]);
yy[y[i]].insert(x[i]);
}
xx[x2].insert(y2);
yy[y2].insert(x2);
queue < pll > q;
q.push(mp(x1, y1));
while(!q.empty()){
pll v = q.front();
q.pop();
vector < ll > xt, yt;
for(ll i : xx[v.fi])
if(!d.count(mp(v.fi, i)))
d[mp(v.fi, i)] = d[v] + 1, q.push(mp(v.fi, i)), xt.pb(i);
for(ll i : yy[v.se])
if(!d.count(mp(i, v.se)))
d[mp(i, v.se)] = d[v] + 1, q.push(mp(i, v.se)), yt.pb(i);
for(ll i : xt) xx[v.fi].erase(i);
for(ll i : yt) yy[v.se].erase(i);
}
if(!d.count(mp(x2, y2)))
printf("-1");
else
printf("%lld", d[mp(x2, y2)] - 1);
return 0;
}
IyBpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgoKdXNpbmcgbmFtZXNwYWNlIH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AgZnJlb3BlbigibGFzZXJzLmluIiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJsYXNlcnMub3V0IiwgInciLCBzdGRvdXQpOwogICAgc2NhbmYoIiVsbGQgJWxsZCAlbGxkICVsbGQgJWxsZCIsICZuLCAmeDEsICZ5MSwgJngyLCAmeTIpOwogICAgZm9yKGxsIGkgPSAxOyBpIDw9IG47ICsraSl7CiAgICAgICAgc2NhbmYoIiVsbGQgJWxsZCIsICZ4W2ldLCAmeVtpXSk7CiAgICAgICAgeHhbeFtpXV0uaW5zZXJ0KHlbaV0pOwogICAgICAgIHl5W3lbaV1dLmluc2VydCh4W2ldKTsKICAgIH0KICAgIHh4W3gyXS5pbnNlcnQoeTIpOwogICAgeXlbeTJdLmluc2VydCh4Mik7CiAgICBxdWV1ZSA8IHBsbCA+IHE7CiAgICBxLnB1c2gobXAoeDEsIHkxKSk7CiAgICB3aGlsZSghcS5lbXB0eSgpKXsKICAgICAgICBwbGwgdiA9IHEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwogICAgICAgIHZlY3RvciA8IGxsID4geHQsIHl0OwogICAgICAgIGZvcihsbCBpIDogeHhbdi5maV0pCiAgICAgICAgICAgIGlmKCFkLmNvdW50KG1wKHYuZmksIGkpKSkKICAgICAgICAgICAgICAgIGRbbXAodi5maSwgaSldID0gZFt2XSArIDEsIHEucHVzaChtcCh2LmZpLCBpKSksIHh0LnBiKGkpOwogICAgICAgIGZvcihsbCBpIDogeXlbdi5zZV0pCiAgICAgICAgICAgIGlmKCFkLmNvdW50KG1wKGksIHYuc2UpKSkKICAgICAgICAgICAgICAgIGRbbXAoaSwgdi5zZSldID0gZFt2XSArIDEsIHEucHVzaChtcChpLCB2LnNlKSksIHl0LnBiKGkpOwogICAgICAgIGZvcihsbCBpIDogeHQpIHh4W3YuZmldLmVyYXNlKGkpOwogICAgICAgIGZvcihsbCBpIDogeXQpIHl5W3Yuc2VdLmVyYXNlKGkpOwogICAgfQogICAgaWYoIWQuY291bnQobXAoeDIsIHkyKSkpCiAgICAgICAgcHJpbnRmKCItMSIpOwogICAgZWxzZQogICAgICAgIHByaW50ZigiJWxsZCIsIGRbbXAoeDIsIHkyKV0gLSAxKTsKICAgIHJldHVybiAwOwp9Cg==