/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
set<ii> valid,visited;
#define x first
#define y second
#define MAX 1000000000
#define bn pair<ii,int>
struct node{
    int x,y;int dist;
    node(){}
    node(int r, int c, int d):x(r),y(c),dist(d){}
};
void bfs(){
    int ans=-1;
    queue<node> q;
    q.push(node(src.x,src.y,0));
    while(!q.empty()){
        node u=q.front();q.pop();
        if(u.x==dest.x&&u.y==dest.y){ans=u.dist;break;}
        visited.insert(ii(u.x,u.y));
        ForInc(k,0,7){
            ii v;v.x=u.x+dz[k][0];v.y=u.y+dz[k][1];
            if(valid.find(v)!=valid.end() && visited.find(v)==visited.end())
                q.push(node(v.x,v.y,u.dist+1));
        }
    }
    pi(ans);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);
        ForInc(j,a,b)valid.insert(ii(r,j));
    }
    bfs();
return 0;
}


#include <cstdio>
#include <queue>
#include <set>
#include <map>

using namespace std;

#define MAXN 100000

set< pair<int, int> > valid,visited;
int dr[] = {-1,-1,-1,0,0,1,1,1};
int dc[] = {-1,0,1,-1,1,-1,0,1};

struct node{
    int r,c,dist;

    node(){}
    node(int _r, int _c, int _dist):
        r(_r), c(_c), dist(_dist){}
};

int main(){
    int r0,c0,r1,c1,n;

    scanf("%d %d %d %d %d",&r0,&c0,&r1,&c1,&n);

    for(int i = 0,r,le,ri;i < n;++i){
        scanf("%d %d %d",&r,&le,&ri);

        for(int j = le;j <= ri;++j)
            valid.insert(make_pair(r,j));
    }

    int ans = -1;
    queue<node> Q;

    Q.push(node(r0,c0,0));
    visited.insert(make_pair(r0,c0));

    while(!Q.empty()){
        node cur = Q.front();
        Q.pop();

        if(cur.r == r1 && cur.c == c1){
            ans = cur.dist;
            break;
        }

        for(int k = 0;k < 8;++k){
            int r2 = cur.r + dr[k];
            int c2 = cur.c + dc[k];

            if(valid.find(make_pair(r2,c2)) != valid.end() && visited.find(make_pair(r2,c2)) == visited.end()){
                visited.insert(make_pair(r2,c2));
                Q.push(node(r2,c2,cur.dist + 1));
            }
        }
    }

    printf("%d\n",ans);

    return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
map<int,row_t> row;
map<ii,int> d;
map<ii,bool> visited;
map<ii,bool> inQ;
#define x first
#define y second
#define MAX 1000000000
void bfs(){
    queue<ii> q;
    q.push(src);d[src]=0;
    while(!q.empty()){
        ii u=q.front();inQ[u]=0;q.pop();
        visited[u]=1;
        ForInc(k,0,7){
            ii v;v.x=u.x+dz[k][0];v.y=u.y+dz[k][1];
            if(row.find(v.x)==row.end()||(v.y<row[v.x].s||v.y>row[v.x].e))continue;
            if(visited.find(v)==visited.end() && (inQ.find(v)==inQ.end()||inQ[v]==1)){
                q.push(v);inQ[v]=1;
                if(d.find(v)==d.end())d[v]=d[u]+1;else d[v]=min(d[v],d[u]+1);
            }
            else if(d[v]>d[u]+1)d[v]=d[u]+1,q.push(v),inQ[v]=1;
        }
    }
    if(d.find(dest)==d.end())printf("-1\n");
    else pi(d[dest]);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);row[r]=row_t(a,b);
    }
    bfs();
return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
map<int,row_t> row;
map<ii,int> d;
map<ii,bool> visited;
#define x first
#define y second
#define MAX 1000000000
void bfs(){
    queue<ii> q;
    q.push(src);d[src]=0;
    while(!q.empty()){
        ii u=q.front();q.pop();
        visited[u]=1;
        ForInc(k,0,7){
            ii v;v.x=u.x+dz[k][0];v.y=u.y+dz[k][1];
            if(v.x<1||v.x>MAX||v.y<1||v.y>MAX||
               (row.find(v.x)==row.end()||(v.y<row[v.x].s||v.y>row[v.x].e)))continue;
            if(visited.find(v)==visited.end()){
                q.push(v);if(d.find(v)==d.end())d[v]=d[u]+1;else d[v]=min(d[v],d[u]+1);
            }
            else if(d[v]>d[u]+1)d[v]=d[u]+1;
        }
    }
    if(d.find(dest)==d.end())printf("-1\n");
    else pi(d[dest]);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);row[r]=row_t(a,b);
    }
    bfs();
return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
map<int,vector<row_t> > row;
map<ii,int> d;
map<ii,bool> visited;
map<ii,bool> inQ;
#define x first
#define y second
#define MAX 1000000000
inline bool check(ii z){
    ForInc(i,0,(int)row[z.x].size()-1){
        if(z.y>=row[z.x][i].s&&z.y<=row[z.x][i].e)return 1;
    }
    return 0;
}
void bfs(){
    queue<ii> q;
    q.push(src);d[src]=0;
    while(!q.empty()){
        ii u=q.front();inQ[u]=0;q.pop();
        if(u==dest)break;
        visited[u]=1;
        ForInc(k,0,7){
            ii v;v.x=u.x+dz[k][0];v.y=u.y+dz[k][1];
            if(row.find(v.x)==row.end()||(!check(v)))continue;
            if(visited.find(v)==visited.end() && (inQ.find(v)==inQ.end()||inQ[v]==1)){
                q.push(v);inQ[v]=1;
                if(d.find(v)==d.end())d[v]=d[u]+1;else d[v]=min(d[v],d[u]+1);
            }
            else if(d[v]>d[u]+1)d[v]=d[u]+1,q.push(v),inQ[v]=1;
        }
    }
    if(d.find(dest)==d.end())printf("-1\n");
    else pi(d[dest]);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);
        row[r].pb(row_t(a,b));
    }
    bfs();
return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
map<int,vector<row_t> > row;
map<ii,bool> visited;
#define x first
#define y second
#define MAX 1000000000
inline bool check(ii z){
    ForInc(i,0,(int)row[z.x].size()-1){
        if(z.y>=row[z.x][i].s&&z.y<=row[z.x][i].e)return 1;
    }
    return 0;
}
#define bn pair<ii,int>
#define coord first
#define dist second
void bfs(){
    int ans=-1;
    queue<bn> q;
    q.push(bn(src,0));
    while(!q.empty()){
        bn u=q.front();q.pop();
        if(u.coord==dest){ans=u.dist;break;}
        visited[u.coord]=1;
        ForInc(k,0,7){
            ii v;v.x=u.coord.x+dz[k][0];v.y=u.coord.y+dz[k][1];
            if(row.find(v.x)==row.end()||(!check(v)))continue;
            if(visited.find(v)==visited.end()){
                q.push(bn(v,u.dist+1));
            }
        }
    }
    pi(ans);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);
        row[r].pb(row_t(a,b));
    }
    bfs();
return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
map<int,vector<row_t> > row;
map<ii,int> d;
map<ii,bool> visited;
map<ii,bool> inQ;
#define x first
#define y second
#define MAX 1000000000
inline bool check(ii z){
    ForInc(i,0,(int)row[z.x].size()-1){
        if(z.y>=row[z.x][i].s&&z.y<=row[z.x][i].e)return 1;
    }
    return 0;
}
void bfs(){
    queue<ii> q;
    q.push(src);d[src]=0;
    while(!q.empty()){
        ii u=q.front();inQ[u]=0;q.pop();
        visited[u]=1;
        ForInc(k,0,7){
            ii v;v.x=u.x+dz[k][0];v.y=u.y+dz[k][1];
            if(row.find(v.x)==row.end()||(!check(v)))continue;
            if(visited.find(v)==visited.end() && (inQ.find(v)==inQ.end()||inQ[v]==1)){
                q.push(v);inQ[v]=1;
                if(d.find(v)==d.end())d[v]=d[u]+1;else d[v]=min(d[v],d[u]+1);
            }
            else if(d[v]>d[u]+1)d[v]=d[u]+1,q.push(v),inQ[v]=1;
        }
    }
    if(d.find(dest)==d.end())printf("-1\n");
    else pi(d[dest]);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);
        row[r].pb(row_t(a,b));
    }
    bfs();
return 0;
}


/*
  Aditya Gourav
  @adi_pearl / @aditya_pearl

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

//scanning
#define si(x) scanf("%d",&x)
#define si2(x,y) scanf("%d%d",&x,&y)
#define si3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define si4(x,y,z,a) scanf("%d%d%d%d",&x,&y,&z,&a)
#define ss(x) scanf("%s",x)
#define sd(x) scanf("%lf",&x)
//debugging
#define db1(msg,x) printf("%s: %d\n",msg,x)
#define db2(msg,x,y) printf("%s: %d %d\n",msg,x,y)
#define db3(msg,x,y,z) printf("%s: %d %d %d\n",msg,x,y,z)
#define db(msg) printf("%s\n",msg)
//printing to console
#define pi(x) printf("%d\n",x)
#define pi2(x,y) printf("%d %d\n",x,y)
#define pill(x) printf("%lld\n",x)
#define pd(x) printf("%lf\n",x)
//others
#define ForInc(var,beg,end) for(int var=beg;var<=end;++var)
#define advForInc(var,beg,end,inc) for(int var=beg;var<=end;var+=inc)
#define ForDec(var,end,beg) for(int var=end;var>=beg;--var)
#define ipArray(arr,size) ForInc(i,0,size-1) si(arr[i]);
#define ipllArray(arr,size) ForInc(i,0,size-1) sill(arr[i]);
#define ii pair<int,int>
#define mp make_pair
#define pb push_back
#define READ(f) freopen(f,"r",stdin);
#define WRITE(f) freopen(f,"w",stdout);
#define TEST int testcases; cin>>testcases;for(;testcases;--testcases)

template<typename T> T gcd(T a, T b) { return (b == 0) ? abs(a) : gcd(b, a % b); }
template<typename T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; }
template<typename T> inline T mod(T a, T b) { return (a % b + b) % b; }
template<typename T> inline T sqr(T x) { return ((x) * (x)); }

const double EPS = 1e-9;
const double BIG = 1e19;
const int INF = INT_MAX;

typedef long long ll;

/* Main Code starts here :) */
ii src,dest;
int dz[][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{-1,1},{1,-1},{-1,-1}};
int n;
struct row_t{
    int s,e;
    row_t(){}
    row_t(int a, int b){s=a;e=b;}
};
set<ii> valid,visited;
#define x first
#define y second
#define MAX 1000000000
#define bn pair<ii,int>
#define coord first
#define dist second
void bfs(){
    int ans=-1;
    queue<bn> q;
    q.push(bn(src,0));
    while(!q.empty()){
        bn u=q.front();q.pop();
        if(u.coord==dest){ans=u.dist;break;}
        visited.insert(u.coord);
        ForInc(k,0,7){
            ii v;v.x=u.coord.x+dz[k][0];v.y=u.coord.y+dz[k][1];
            if(valid.find(v)!=valid.end() && visited.find(v)==visited.end())
                q.push(bn(v,u.dist+1));
        }
    }
    pi(ans);
}
int main(){
    int a,b,r;
    si4(src.x,src.y,dest.x,dest.y);
    si(n);
    ForInc(i,1,n){
        si3(r,a,b);
        ForInc(j,a,b)valid.insert(ii(r,j));
    }
    bfs();
return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#include <sstream>

#define clr(name,val) memset(name,val,sizeof(name));
#define EPS .000000001
#define ll long long
#define psb(b) push_back(b)
#define ppb() pop_back()
#define oo 100000000000000000LL
#define swap(x,y) {int t;t=x;x=y;y=t;}
#define for_i(s,n) for(int i=s;i<n;i++)
#define for_j(s,n) for(int j=s;j<n;j++)
#define for_k(s,n) for(int k=s;k<n;k++)
#define MAX 100099

///next_permutation  next_permutation (s.begin(),s.end())
///reverse(a,a+n);
///binary_search(first,last);
///vector erase v.erase(v.begin()+position);
///map map<int , int > data;
///map clear  data.clear();
///map iterator>>>> map <int,vector <int> >::const_iterator it;
///find an element in map (colour.find(nd)==colour.end());//if it return true this
///mean the element is'nt in the map.
///pass a vector to a funtion: funtion (vector <data type> &vector name);
///make_pair  point=make_pair(i,j);
///access pair value point.first;point.second;

using namespace std;

///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
ll rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
ll cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
///int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
///int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

ll arr[MAX][2];

map<pair<ll,ll>,ll> data;

ll bfs(ll s1,ll s2,ll e1,ll e2)
{
    ll c,r;
    c=r=1000000001L;
    queue<ll> Q;
    Q.push(s1);Q.push(s2);
    pair<ll , ll> mpoint,point;
    mpoint = make_pair(s1,s2);
    data[point]=0L;
    while(!Q.empty())
    {
        s1=Q.front();Q.pop();
        s2=Q.front();Q.pop();
        mpoint=make_pair(s1,s2);
        for(int i=0;i<8;i++)
        {
            ll t1=s1+rr[i];
            ll t2=s2+cc[i];
            if(arr[t1][1]!=-1)
            {
                if((t1>-1&&t1<r)&&(t2>=arr[t1][0]&&t2<=arr[t1][1]))
                {
                    point = make_pair(t1,t2);
                    if(data.find(point)==data.end())
                    {
                        data[point]=data[mpoint]+1L;
                        if(t1==e1&&t2==e2) return data[point];
                        Q.push(t1);
                        Q.push(t2);
                    }
                }
            }
        }
    }
    return -1L;
}

int main()
{
    for(int i=0;i<MAX;i++) arr[i][0]=oo,arr[i][1]=-1;
    ll x1,y1,x2,y2,r,a,b;
    cin>>x1>>y1>>x2>>y2;
    int n;
    cin>>n;
    int node=n;
    while(node--)
    {
        cin>>r>>a>>b;
        arr[r][0]=arr[r][0]<a?arr[r][0]:a;
        arr[r][1]=arr[r][1]<b?b:arr[r][1];
    }
    ll cnt=bfs(x1,y1,x2,y2);
    if(cnt!=-1) cout<<cnt<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#include <sstream>

#define clr(name,val) memset(name,val,sizeof(name));
#define EPS .000000001
#define ll long long
#define psb(b) push_back(b)
#define ppb() pop_back()
#define oo 100000000000000000LL
#define swap(x,y) {int t;t=x;x=y;y=t;}
#define for_i(s,n) for(int i=s;i<n;i++)
#define for_j(s,n) for(int j=s;j<n;j++)
#define for_k(s,n) for(int k=s;k<n;k++)
#define MAX 100099

///next_permutation  next_permutation (s.begin(),s.end())
///reverse(a,a+n);
///binary_search(first,last);
///vector erase v.erase(v.begin()+position);
///map map<int , int > data;
///map clear  data.clear();
///map iterator>>>> map <int,vector <int> >::const_iterator it;
///find an element in map (colour.find(nd)==colour.end());//if it return true this
///mean the element is'nt in the map.
///pass a vector to a funtion: funtion (vector <data type> &vector name);
///make_pair  point=make_pair(i,j);
///access pair value point.first;point.second;

using namespace std;

///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
ll rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
ll cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
///int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
///int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

ll arr[MAX][2];

map<pair<ll,ll>,ll> data;

ll bfs(ll s1,ll s2,ll e1,ll e2)
{
    ll c,r;
    c=r=1000000001L;
    queue<ll> Q;
    Q.push(s1);Q.push(s2);
    pair<ll , ll> mpoint,point;
    mpoint = make_pair(s1,s2);
    data[point]=0L;
    while(!Q.empty())
    {
        s1=Q.front();Q.pop();
        s2=Q.front();Q.pop();
        mpoint=make_pair(s1,s2);
        for(int i=0;i<8;i++)
        {
            ll t1=s1+rr[i];
            ll t2=s2+cc[i];
            if(arr[t1][1]!=-1L)
            {
                if((t1>-1L&&t1<r)&&(t2>=arr[t1][0]&&t2<=arr[t1][1]))
                {
                    point = make_pair(t1,t2);
                    if(data.find(point)==data.end())
                    {
                        data[point]=data[mpoint]+1L;
                        if(t1==e1&&t2==e2) return data[point];
                        Q.push(t1);
                        Q.push(t2);
                    }
                }
            }
        }
    }
    return -1L;
}

int main()
{
    for(int i=0;i<MAX;i++) arr[i][0]=oo,arr[i][1]=-1;
    ll x1,y1,x2,y2,r,a,b;
    cin>>x1>>y1>>x2>>y2;
    int n;
    cin>>n;
    int node=n;
    while(node--)
    {
        cin>>r>>a>>b;
        arr[r][0]=arr[r][0]<a?arr[r][0]:a;
        arr[r][1]=arr[r][1]<b?b:arr[r][1];
    }
    ll cnt=bfs(x1,y1,x2,y2);
    if(cnt!=-1L) cout<<cnt<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#include <sstream>

#define clr(name,val) memset(name,val,sizeof(name));
#define EPS .000000001
#define ll long long
#define psb(b) push_back(b)
#define ppb() pop_back()
#define oo 100000000000000000LL
#define swap(x,y) {int t;t=x;x=y;y=t;}
#define for_i(s,n) for(int i=s;i<n;i++)
#define for_j(s,n) for(int j=s;j<n;j++)
#define for_k(s,n) for(int k=s;k<n;k++)
#define MAX 100099

///next_permutation  next_permutation (s.begin(),s.end())
///reverse(a,a+n);
///binary_search(first,last);
///vector erase v.erase(v.begin()+position);
///map map<int , int > data;
///map clear  data.clear();
///map iterator>>>> map <int,vector <int> >::const_iterator it;
///find an element in map (colour.find(nd)==colour.end());//if it return true this
///mean the element is'nt in the map.
///pass a vector to a funtion: funtion (vector <data type> &vector name);
///make_pair  point=make_pair(i,j);
///access pair value point.first;point.second;

using namespace std;

///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
ll rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
ll cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
///int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
///int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

map<pair<ll,ll>,ll> data;
map<ll,vector<pair<ll,ll> > >arr;

bool cheak(ll t1,ll t2)
{
    for(int i=0;i<arr[t1].size();i++)
    {
        if(t2>=arr[t1][i].first&&t2<=arr[t1][i].second) return true;
    }
    return false;
}

ll bfs(ll s1,ll s2,ll e1,ll e2)
{
    ll c,r;
    c=r=1000000001L;
    queue<ll> Q;
    Q.push(s1);Q.push(s2);
    pair<ll , ll> mpoint,point;
    mpoint = make_pair(s1,s2);
    data[point]=0L;
    while(!Q.empty())
    {
        s1=Q.front();Q.pop();
        s2=Q.front();Q.pop();
        mpoint=make_pair(s1,s2);
        for(int i=0;i<8;i++)
        {
            ll t1=s1+(ll) rr[i];
            ll t2=s2+(ll) cc[i];
            point = make_pair(t1,t2);
            if(arr.find(t1)==arr.end()) continue;
            else
            {
                if((t1>-1L&&t1<r)&&cheak(t1,t2)/*(t2>=arr[t1].first&&t2<=arr[t1].second)*/)
                {
                    if(data.find(point)==data.end())
                    {
                        data[point]=data[mpoint]+1L;
                        if(t1==e1&&t2==e2) return data[point];
                        Q.push(t1);
                        Q.push(t2);
                    }
                }
            }
        }
    }
    return -1L;
}

int main()
{
    ll x1,y1,x2,y2,r,a,b;
    cin>>x1>>y1>>x2>>y2;
    int n;
    cin>>n;
    int node=n;
    pair<ll,ll> point;
    while(node--)
    {
        cin>>r>>a>>b;
        point = make_pair(a,b);
        arr[r].psb(point);
    }
    ll cnt=bfs(x1,y1,x2,y2);
    if(cnt!=-1L) cout<<cnt<<endl;
    else cout<<-1<<endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cstdlib>
#include <cstring>
#include <string>
#include <ctime>
#include <sstream>

#define clr(name,val) memset(name,val,sizeof(name));
#define EPS .000000001
#define ll long long
#define psb(b) push_back(b)
#define ppb() pop_back()
#define oo 100000000000000000LL
#define swap(x,y) {int t;t=x;x=y;y=t;}
#define for_i(s,n) for(int i=s;i<n;i++)
#define for_j(s,n) for(int j=s;j<n;j++)
#define for_k(s,n) for(int k=s;k<n;k++)
#define MAX 100099

///next_permutation  next_permutation (s.begin(),s.end())
///reverse(a,a+n);
///binary_search(first,last);
///vector erase v.erase(v.begin()+position);
///map map<int , int > data;
///map clear  data.clear();
///map iterator>>>> map <int,vector <int> >::const_iterator it;
///find an element in map (colour.find(nd)==colour.end());//if it return true this
///mean the element is'nt in the map.
///pass a vector to a funtion: funtion (vector <data type> &vector name);
///make_pair  point=make_pair(i,j);
///access pair value point.first;point.second;

using namespace std;

///int rr[]= {-1,-1,0,0,1,1};
///int cc[]= {-1,0,-1,1,0,1};
///int rr[]= {0,0,1,-1};/*4 side move*/
///int cc[]= {-1,1,0,0};/*4 side move*/
ll rr[]= {1,1,0,-1,-1,-1,0,1};/*8 side move*/
ll cc[]= {0,1,1,1,0,-1,-1,-1};/*8 side move*/
///int rr[]={1,1,2,2,-1,-1,-2,-2};/*night move*/
///int cc[]={2,-2,1,-1,2,-2,1,-1};/*night move*/

map<pair<ll,ll>,ll> data;
map<ll,pair<ll,ll> >arr;

ll bfs(ll s1,ll s2,ll e1,ll e2)
{
    ll c,r;
    c=r=1000000001L;
    queue<ll> Q;
    Q.push(s1);Q.push(s2);
    pair<ll , ll> mpoint,point;
    mpoint = make_pair(s1,s2);
    data[point]=0L;
    while(!Q.empty())
    {
        s1=Q.front();Q.pop();
        s2=Q.front();Q.pop();
        mpoint=make_pair(s1,s2);
        for(int i=0;i<8;i++)
        {
            ll t1=s1+(ll) rr[i];
            ll t2=s2+(ll) cc[i];
            point = make_pair(t1,t2);
            if(arr.find(t1)==arr.end()) continue;
            else
            {
                if((t1>-1L&&t1<r)&&(t2>=arr[t1].first&&t2<=arr[t1].second))
                {
                    if(data.find(point)==data.end())
                    {
                        data[point]=data[mpoint]+1L;
                        if(t1==e1&&t2==e2) return data[point];
                        Q.push(t1);
                        Q.push(t2);
                    }
                }
            }
        }
    }
    return -1L;
}

int main()
{
    ll x1,y1,x2,y2,r,a,b;
    cin>>x1>>y1>>x2>>y2;
    int n;
    cin>>n;
    int node=n;
    pair<ll,ll> point;
    while(node--)
    {
        cin>>r>>a>>b;
        point = make_pair(a,b);
        if(arr.find(r)==arr.end())
        {
            arr[r]=point;
            continue;
        }
        arr[r].first=arr[r].first<a?arr[r].first:a;
        arr[r].second=arr[r].second<b?b:arr[r].second;
    }
    ll cnt=bfs(x1,y1,x2,y2);
    if(cnt!=-1L) cout<<cnt<<endl;
    else cout<<-1<<endl;
    return 0;
}


//Boleyn Su's Template for Codeforces
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std;

#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repf(i,0,n)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=b;--i)
#define feach(e,s,t) for (t::itr e=(s).begin();e!=(s).end();++e)
#define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set)
#define forin(i,charset) for (char const* i=charset;*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,0)
#define cpy(x,y) memcpy((x),(y),sizeof(x))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) (x).resize(unique(all(x))-x.begin())
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define sf scanf
#define pf printf
#define pdb(prcs,x) printf("%."#prcs"f",(abs(x)<1e-##prcs)?0.0:x)
#define prt(x) cout<<#x<<"="<<(x)<<endl
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() printf("Used: %.3fs\n",db(clock())/CLOCKS_PER_SEC);

typedef long long int lli;
typedef double db;
typedef char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef pr<str,int> psi;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;
typedef prq<int> pqi;

const int oo=(~0u)>>1;
const lli ooll=(~0ull)>>1;
const db inf=1e+20;
const db eps=1e-8;
const db pi=acos(-1.0);
const int MOD=1000000007;

template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);}
int sgn(const db& x){rtn dbcmp(x,0);}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& set){rep(i,sz(set))set[i]=i;}
inline int find_set(vi& set,int x){int y=x,z;whl(y!=set[y])y=set[y];whl(x!=set[x])z=set[x],set[x]=y,x=z;rtn y;}
inline bool union_set(vi& set,int a,int b){a=find_set(set,a),b=find_set(set,b);rtn a!=b?set[a]=b,true:false;}

const int ds=8;
const int dx[ds]={-1,-1,-1,0,0,1,1,1};
const int dy[ds]={-1,0,1,-1,1,-1,0,1};

int pss;
pii ps[1000000];
int qh,qt;
int q[1000000];
int d[1000000];

int main()
{
	int x0,y0,x1,y1;
	scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
	ps[pss++]=(mp(x0,y0));
	ps[pss++]=(mp(x1,y1));
	int n;
	scanf("%d",&n);
	rep(i,n)
	{
		int r,a,b;
		scanf("%d%d%d",&r,&a,&b);
		ft(i,a,b) ps[pss++]=(mp(r,i));
	}
	sort(ps,ps+pss);
	pss=unique(ps,ps+pss)-ps;
	int s=find(ps,ps+pss,mp(x0,y0))-ps,t=find(ps,ps+pss,mp(x1,y1))-ps;
	q[qh]=s;
	d[s]=1;
	whl(qh<=qt)
	{
		int x=q[qh++];
		rep(i,ds)
		{
			pii p=mp(ps[x].x+dx[i],ps[x].y+dy[i]);
			int y=lower_bound(ps,ps+pss,p)-ps;
			if (y<pss&&ps[y]==p&&!d[y])
			{
				if (y==t)
				{
					printf("%d\n",d[x]);
					rtn 0;
				}
				q[++qt]=y;
				d[y]=d[x]+1;
			}
		}
	}
	puts("-1");
}


//Boleyn Su's Template for Codeforces
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std;

#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repf(i,0,n)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=b;--i)
#define feach(e,s,t) for (t::itr e=(s).begin();e!=(s).end();++e)
#define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set)
#define forin(i,charset) for (char const* i=charset;*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,0)
#define cpy(x,y) memcpy((x),(y),sizeof(x))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) (x).resize(unique(all(x))-x.begin())
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define sf scanf
#define pf printf
#define pdb(prcs,x) printf("%."#prcs"f",(abs(x)<1e-##prcs)?0.0:x)
#define prt(x) cout<<#x<<"="<<(x)<<endl
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() printf("Used: %.3fs\n",db(clock())/CLOCKS_PER_SEC);

typedef long long int lli;
typedef double db;
typedef char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef pr<str,int> psi;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;
typedef prq<int> pqi;

const int oo=(~0u)>>1;
const lli ooll=(~0ull)>>1;
const db inf=1e+20;
const db eps=1e-8;
const db pi=acos(-1.0);
const int MOD=1000000007;

template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);}
int sgn(const db& x){rtn dbcmp(x,0);}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& set){rep(i,sz(set))set[i]=i;}
inline int find_set(vi& set,int x){int y=x,z;whl(y!=set[y])y=set[y];whl(x!=set[x])z=set[x],set[x]=y,x=z;rtn y;}
inline bool union_set(vi& set,int a,int b){a=find_set(set,a),b=find_set(set,b);rtn a!=b?set[a]=b,true:false;}

const int ds=8;
const int dx[ds]={-1,-1,-1,0,0,1,1,1};
const int dy[ds]={-1,0,1,-1,1,-1,0,1};

int main()
{
	vec<pii> ps;
	int x0,y0,x1,y1;
	cin>>x0>>y0>>x1>>y1;
	ps.pb(mp(x0,y0));
	ps.pb(mp(x1,y1));
	int n;
	cin>>n;
	rep(i,n)
	{
		int r,a,b;
		cin>>r>>a>>b;
		ft(i,a,b) ps.pb(mp(r,i));
	}
	srt(ps);
	uniq(ps);
	queue<int> q;
	vi d(sz(ps));
	int s=find(all(ps),mp(x0,y0))-ps.begin(),t=find(all(ps),mp(x1,y1))-ps.begin();
	q.push(s);
	d[s]=1;
	whl(!q.empty())
	{
		if (q.front()==t)
		{
			cout<<d[t]-1<<endl;
			rtn 0;
		}
		int x=q.front();
		q.pop();
		pii p=ps[x];
		rep(i,ds)
		{
			p.x+=dx[i],p.y+=dy[i];
			int y=find(all(ps),p)-ps.begin();
			if (y<sz(d)&&!d[y])
			{
				q.push(y);
				d[y]=d[x]+1;
			}
			p.x-=dx[i],p.y-=dy[i];
		}
	}
	cout<<-1<<endl;
}


//Boleyn Su's Template for Codeforces
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std;

#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repf(i,0,n)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=b;--i)
#define feach(e,s,t) for (t::itr e=(s).begin();e!=(s).end();++e)
#define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set)
#define forin(i,charset) for (char const* i=charset;*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,0)
#define cpy(x,y) memcpy((x),(y),sizeof(x))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) (x).resize(unique(all(x))-x.begin())
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define sf scanf
#define pf printf
#define pdb(prcs,x) printf("%."#prcs"f",(abs(x)<1e-##prcs)?0.0:x)
#define prt(x) cout<<#x<<"="<<(x)<<endl
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() printf("Used: %.3fs\n",db(clock())/CLOCKS_PER_SEC);

typedef long long int lli;
typedef double db;
typedef char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef pr<str,int> psi;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;
typedef prq<int> pqi;

const int oo=(~0u)>>1;
const lli ooll=(~0ull)>>1;
const db inf=1e+20;
const db eps=1e-8;
const db pi=acos(-1.0);
const int MOD=1000000007;

template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);}
int sgn(const db& x){rtn dbcmp(x,0);}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& set){rep(i,sz(set))set[i]=i;}
inline int find_set(vi& set,int x){int y=x,z;whl(y!=set[y])y=set[y];whl(x!=set[x])z=set[x],set[x]=y,x=z;rtn y;}
inline bool union_set(vi& set,int a,int b){a=find_set(set,a),b=find_set(set,b);rtn a!=b?set[a]=b,true:false;}

const int ds=8;
const int dx[ds]={-1,-1,-1,0,0,1,1,1};
const int dy[ds]={-1,0,1,-1,1,-1,0,1};

int pss;
pii ps[1000000];
int qh,qt;
int q[1000000];
int d[1000000];

int main()
{
	int x0,y0,x1,y1;
	cin>>x0>>y0>>x1>>y1;
	ps[pss++]=(mp(x0,y0));
	ps[pss++]=(mp(x1,y1));
	int n;
	cin>>n;
	rep(i,n)
	{
		int r,a,b;
		cin>>r>>a>>b;
		ft(i,a,b) ps[pss++]=(mp(r,i));
	}
	sort(ps,ps+pss);
	pss=unique(ps,ps+pss)-ps;
	int s=find(ps,ps+pss,mp(x0,y0))-ps,t=find(ps,ps+pss,mp(x1,y1))-ps;
	q[qh]=s;
	d[s]=1;
	whl(qh<=qt)
	{
		if (q[qh]==t)
		{
			cout<<d[t]-1<<endl;
			rtn 0;
		}
		int x=q[qh];
		qh++;
		pii p=ps[x];
		rep(i,ds)
		{
			p.x+=dx[i],p.y+=dy[i];
			int y=find(ps,ps+pss,p)-ps;
			if (y<pss&&!d[y])
			{
				q[++qt]=y;
				d[y]=d[x]+1;
			}
			p.x-=dx[i],p.y-=dy[i];
		}
	}
	cout<<-1<<endl;
}


//Boleyn Su's Template for Codeforces
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <climits>
using namespace std;

#define lp for(;;)
#define repf(i,a,b) for (int i=(a);i<(b);++i)
#define rep(i,n) repf(i,0,n)
#define ft(i,a,b) for (int i=(a);i<=(b);++i)
#define fdt(i,a,b) for (int i=(a);i>=b;--i)
#define feach(e,s,t) for (t::itr e=(s).begin();e!=(s).end();++e)
#define fsubset(subset,set) for (int subset=set&(set-1);subset;subset=(subset-1)&set)
#define forin(i,charset) for (char const* i=charset;*i;i++)
#define whl while
#define rtn return
#define fl(x,y) memset((x),char(y),sizeof(x))
#define clr(x) fl(x,0)
#define cpy(x,y) memcpy((x),(y),sizeof(x))
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define all(x) (x).begin(),(x).end()
#define srt(x) sort(all(x))
#define uniq(x) (x).resize(unique(all(x))-x.begin())
#define vec vector
#define pr pair
#define que queue
#define prq priority_queue
#define itr iterator
#define sf scanf
#define pf printf
#define pdb(prcs,x) printf("%."#prcs"f",(abs(x)<1e-##prcs)?0.0:x)
#define prt(x) cout<<#x<<"="<<(x)<<endl
#define asrtWA(s) do if(!(s))exit(0);whl(0)
#define asrtTLE(s) do if(!(s))whl(1);whl(0)
#define asrtMLE(s) do if(!(s))whl(new int);whl(0)
#define asrtOLE(s) do if(!(s))whl(1)puts("OLE");whl(0)
#define asrtRE(s) do if(!(s))*(int*)0=0;whl(0)
#define runtime() printf("Used: %.3fs\n",db(clock())/CLOCKS_PER_SEC);

typedef long long int lli;
typedef double db;
typedef char* cstr;
typedef string str;
typedef vec<int> vi;
typedef vec<vi> vvi;
typedef vec<bool> vb;
typedef vec<vb> vvb;
typedef vec<str> vs;
typedef pr<int,int> pii;
typedef pr<lli,lli> pll;
typedef pr<db,db> pdd;
typedef pr<str,int> psi;
typedef map<int,int> mii;
typedef map<str,int> msi;
typedef map<char,int> mci;
typedef set<int> si;
typedef set<str> ss;
typedef que<int> qi;
typedef prq<int> pqi;

const int oo=(~0u)>>1;
const lli ooll=(~0ull)>>1;
const db inf=1e+20;
const db eps=1e-8;
const db pi=acos(-1.0);
const int MOD=1000000007;

template<typename type>inline bool cmax(type& a,const type& b){rtn a<b?a=b,true:false;}
template<typename type>inline bool cmin(type& a,const type& b){rtn b<a?a=b,true:false;}
template<typename type>inline type sqr(const type& x){rtn x*x;}
int dbcmp(const db& a,const db& b){rtn (a>b+eps)-(a<b-eps);}
int sgn(const db& x){rtn dbcmp(x,0);}
template<typename type>inline type gcd(type a,type b){if(b)whl((a%=b)&&(b%=a));rtn a+b;}
template<typename type>inline type lcm(type a,type b){rtn a*b/gcd(a,b);}
template<typename type>inline void bit_inc(vec<type>& st,int x,type inc){whl(x<sz(st))st[x]+=inc,x|=x+1;}
template<typename type>inline type bit_sum(const vec<type>& st,int x){type s=0;whl(x>=0)s+=st[x],x=(x&(x+1))-1;rtn s;}
template<typename type>inline type bit_kth(const vec<type>& st,int k){int x=0,y=0,z=0;whl((1<<(++y))<sz(st));fdt(i,y-1,0){if((x+=1<<i)>sz(st)||z+st[x-1]>k)x-=1<<i;else z+=st[x-1];}rtn x;}
inline void make_set(vi& set){rep(i,sz(set))set[i]=i;}
inline int find_set(vi& set,int x){int y=x,z;whl(y!=set[y])y=set[y];whl(x!=set[x])z=set[x],set[x]=y,x=z;rtn y;}
inline bool union_set(vi& set,int a,int b){a=find_set(set,a),b=find_set(set,b);rtn a!=b?set[a]=b,true:false;}

const int ds=8;
const int dx[ds]={-1,-1,-1,0,0,1,1,1};
const int dy[ds]={-1,0,1,-1,1,-1,0,1};

int pss;
pii ps[1000000];
int qh,qt;
int q[1000000];
int d[1000000];

int main()
{
	int x0,y0,x1,y1;
	scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
	ps[pss++]=(mp(x0,y0));
	ps[pss++]=(mp(x1,y1));
	int n;
	scanf("%d",&n);
	rep(i,n)
	{
		int r,a,b;
		scanf("%d%d%d",&r,&a,&b);
		ft(i,a,b) ps[pss++]=(mp(r,i));
	}
	sort(ps,ps+pss);
	pss=unique(ps,ps+pss)-ps;
	asrtOLE(pss<=100000+2);
	int s=find(ps,ps+pss,mp(x0,y0))-ps,t=find(ps,ps+pss,mp(x1,y1))-ps;
	q[qh]=s;
	d[s]=1;
	whl(qh<=qt)
	{
		if (q[qh]==t)
		{
			cout<<d[t]-1<<endl;
			rtn 0;
		}
		int x=q[qh];
		qh++;
		pii p=ps[x];
		rep(i,ds)
		{
			p.x+=dx[i],p.y+=dy[i];
			int y=find(ps,ps+pss,p)-ps;
			if (y<pss&&!d[y])
			{
				q[++qt]=y;
				d[y]=d[x]+1;
			}
			p.x-=dx[i],p.y-=dy[i];
		}
	}
	cout<<-1<<endl;
}


#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long LL;
#define pole(x,y) pole[make_pair(x,y)]
#define was(x,y) was[make_pair(x,y)]

int main()
{
 freopen("input.txt","r",stdin);
 LL x0,y0,x1,y1;
 LL n;
 LL  r,a,b;
 map <pair<LL,LL>, bool > pole;
 map <pair<LL,LL>, int > was;

// pole(1,2)= false;
 cin>>x0>>y0>>x1>>y1;
 cin>>n;
 for (int i=0; i<n; i++)
 {
  cin>>r>>a>>b;
  for (int j= a; j<=b; j++)
   pole(r,j)= true;
 }

 was(x0,y0)= 1;

 vector <pair <LL,LL> > list;

 list.push_back(make_pair(x0,y0));

 pair <LL,LL> search= make_pair(x1,y1);
 int i=0;
 LL x,y,k=0;

/* for (int i=0; i<15; i++)
 {
  for (int j=0; j<15; j++)
   cout<<was(i,j);
  cout<<endl;
 }
*/
 while (i<list.size() && list[i]!=search)
 {
  x= list[i].first; y= list[i].second; k= was(x,y)+1;
//  cout<<x<<" "<<y<<"   "<<pole(x,y+1)<<"  "<<was(x,y+1)<<endl;
# define pbp(x,y) push_back(make_pair(x,y))
  if (pole(x+1,y) && was(x+1,y)==0) { list.pbp(x+1,y); was(x+1,y)= k;}
  if (pole(x-1,y) && was(x-1,y)==0) { list.pbp(x-1,y); was(x-1,y)= k;}
  if (pole(x,y+1) && was(x,y+1)==0) { list.pbp(x,y+1); was(x,y+1)= k;}
  if (pole(x,y-1) && was(x,y-1)==0) { list.pbp(x,y-1); was(x,y-1)= k;}

  if (pole(x+1,y+1) && was(x+1,y+1)==0) { list.pbp(x+1,y+1); was(x+1,y+1)= k;}
  if (pole(x-1,y+1) && was(x-1,y+1)==0) { list.pbp(x-1,y+1); was(x-1,y+1)= k;}
  if (pole(x+1,y-1) && was(x+1,y-1)==0) { list.pbp(x+1,y-1); was(x+1,y-1)= k;}
  if (pole(x-1,y-1) && was(x-1,y-1)==0) { list.pbp(x-1,y-1); was(x-1,y-1)= k;}

 i++;
 }

if (i>=list.size()) cout<<-1<<endl;
else cout<<(was(list[i].first,list[i].second)-1)<<endl;
 return 0;
}


#include <iostream>
#include <algorithm>
#include <stdio.h>

using namespace std;

 pair <long, long> kletki[100010];
 long Min [100010];

# define Max 1000000000
 long n;

long find(long x, long y)
{
 if (x>Max || y>Max || x<1 || y<1) return 0;
 long q=1, w=n;
 long m;
 while (w>q)
 {
  m= (w+q)/2;
  if (kletki[m].first>x) {w=m-1; continue;}
  if (kletki[m].first<x) {q=m+1; continue;}
  if (kletki[m].second>y) {w=m-1; continue; }
  if (kletki[m].second<y) {q=m+1; continue; }
 }
 if (kletki[m].first==x && kletki[m].second==y) return w;
 else return 0;
}

long rec(long k)
{
 if (Min[k]>-1) return Min[k];
 if (Min[k]==-2) return Max;
 Min[k]= -2;
 long x=kletki[k].first;
 long y=kletki[k].second;
 long MIN;
 MIN= min(rec(find(x-1,y-1)),rec(find(x-1,y)));
 MIN= min(MIN,rec(find(x-1,y+1)));
 MIN= min(MIN,rec(find(x,y-1)));
 MIN= min(MIN,rec(find(x,y+1)));
 MIN= min(MIN,rec(find(x+1,y-1)));
 MIN= min(MIN,rec(find(x+1,y)));
 MIN= min(MIN,rec(find(x+1,y+1)));
 Min[k]= MIN+1;
 return Min[k];
 //,rec(find(x,y-1)),rec(find(x,y+1)),rec(find(x+1,y-1)),rec(find(x+1,y)),rec(find(x+1,y+1)));
}

int main()
{
 long x0,y0,x1,y1;
 cin >> x0 >> y0 >> x1 >> y1;
 cin >> n;
 long r,a,b;
 long kol=0;
 for (long i=1; i<= n; i++)
 {
  cin >> r >> a >> b;
  for (long j=a; j<= b; j++)
  {
      kletki[++kol]= make_pair(r,j);
  }
 }
 sort(kletki+1,kletki+kol+1);

 memset(Min,-1,sizeof(Min));

 Min[find(x0,y0)]=0;
 Min[0]= Max;

 long ans;
 ans= rec(find(x1,y1));

 if (ans<Max) cout << ans << endl;
 else cout << "-1" << endl;
 return 0;
}


#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long LL;
#define pole(x,y) pole[(1000000000LL*(x)+y)]
#define was(x,y) was[(1000000000LL*(x)+y)]

int main()
{
 //freopen("input.txt","r",stdin);
 LL x0,y0,x1,y1;
 LL n;
 LL  r,a,b;
 map <LL, bool > pole;
 map <LL, int > was;

// pole(1,2)= false;
 cin>>x0>>y0>>x1>>y1;
 cin>>n;
 for (int i=0; i<n; i++)
 {
  cin>>r>>a>>b;
  for (int j= a; j<=b; j++)
   pole(r,j)= true;
 }

 was(x0,y0)= 1;

 vector <pair <LL,LL> > list;

 list.push_back(make_pair(x0,y0));

 pair <LL,LL> search= make_pair(x1,y1);
 int i=0;
 LL x,y,k=0;
/*
 for (int i=0; i<15; i++)
 {
  for (int j=0; j<15; j++)
   cout<<was(i,j);
  cout<<endl;
 }
*/
 while (i<list.size() && list[i]!=search)
 {
  x= list[i].first; y= list[i].second; k= was(x,y)+1;
  //cout<<x<<" "<<y<<"   "<<pole(x,y+1)<<"  "<<was(x,y+1)<<endl;
# define pbp(x,y) push_back(make_pair(x,y))
  if (pole(x+1,y) && was(x+1,y)==0) { list.pbp(x+1,y); was(x+1,y)= k;}
  if (pole(x-1,y) && was(x-1,y)==0) { list.pbp(x-1,y); was(x-1,y)= k;}
  if (pole(x,y+1) && was(x,y+1)==0) { list.pbp(x,y+1); was(x,y+1)= k;}
  if (pole(x,y-1) && was(x,y-1)==0) { list.pbp(x,y-1); was(x,y-1)= k;}

  if (pole(x+1,y+1) && was(x+1,y+1)==0) { list.pbp(x+1,y+1); was(x+1,y+1)= k;}
  if (pole(x-1,y+1) && was(x-1,y+1)==0) { list.pbp(x-1,y+1); was(x-1,y+1)= k;}
  if (pole(x+1,y-1) && was(x+1,y-1)==0) { list.pbp(x+1,y-1); was(x+1,y-1)= k;}
  if (pole(x-1,y-1) && was(x-1,y-1)==0) { list.pbp(x-1,y-1); was(x-1,y-1)= k;}

 i++;
 }

if (i>=list.size()) cout<<-1<<endl;
else cout<<(was(list[i].first,list[i].second)-1)<<endl;
 return 0;
}


#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long LL;
#define pole(x,y) pole[(1000000000LL*x+y)]
#define was(x,y) was[(1000000000LL*x+y)]

int main()
{
 //freopen("input.txt","r",stdin);
 LL x0,y0,x1,y1;
 LL n;
 LL  r,a,b;
 map <LL, bool > pole;
 map <LL, int > was;

// pole(1,2)= false;
 cin>>x0>>y0>>x1>>y1;
 cin>>n;
 for (int i=0; i<n; i++)
 {
  cin>>r>>a>>b;
  for (int j= a; j<=b; j++)
   pole(r,j)= true;
 }

 was(x0,y0)= 1;

 vector <pair <LL,LL> > list;

 list.push_back(make_pair(x0,y0));

 pair <LL,LL> search= make_pair(x1,y1);
 int i=0;
 LL x,y,k=0;

/* for (int i=0; i<15; i++)
 {
  for (int j=0; j<15; j++)
   cout<<was(i,j);
  cout<<endl;
 }
*/
 while (i<list.size() && list[i]!=search)
 {
  x= list[i].first; y= list[i].second; k= was(x,y)+1;
//  cout<<x<<" "<<y<<"   "<<pole(x,y+1)<<"  "<<was(x,y+1)<<endl;
# define pbp(x,y) push_back(make_pair(x,y))
  if (pole(x+1,y) && was(x+1,y)==0) { list.pbp(x+1,y); was(x+1,y)= k;}
  if (pole(x-1,y) && was(x-1,y)==0) { list.pbp(x-1,y); was(x-1,y)= k;}
  if (pole(x,y+1) && was(x,y+1)==0) { list.pbp(x,y+1); was(x,y+1)= k;}
  if (pole(x,y-1) && was(x,y-1)==0) { list.pbp(x,y-1); was(x,y-1)= k;}

  if (pole(x+1,y+1) && was(x+1,y+1)==0) { list.pbp(x+1,y+1); was(x+1,y+1)= k;}
  if (pole(x-1,y+1) && was(x-1,y+1)==0) { list.pbp(x-1,y+1); was(x-1,y+1)= k;}
  if (pole(x+1,y-1) && was(x+1,y-1)==0) { list.pbp(x+1,y-1); was(x+1,y-1)= k;}
  if (pole(x-1,y-1) && was(x-1,y-1)==0) { list.pbp(x-1,y-1); was(x-1,y-1)= k;}

 i++;
 }

if (i>=list.size()) cout<<-1<<endl;
else cout<<(was(list[i].first,list[i].second)-1)<<endl;
 return 0;
}


#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef long long LL;
#define pole(x,y) pole[make_pair(x,y)]
#define was(x,y) was[make_pair(x,y)]

int main()
{
 //freopen("input.txt","r",stdin);
 LL x0,y0,x1,y1;
 LL n;
 LL  r,a,b;
 map <pair<LL,LL>, bool > pole;
 map <pair<LL,LL>, int > was;

// pole(1,2)= false;
 cin>>x0>>y0>>x1>>y1;
 cin>>n;
 for (int i=0; i<n; i++)
 {
  cin>>r>>a>>b;
  for (int j= a; j<=b; j++)
   pole(r,j)= true;
 }

 was(x0,y0)= 1;

 vector <pair <LL,LL> > list;

 list.push_back(make_pair(x0,y0));

 pair <LL,LL> search= make_pair(x1,y1);
 int i=0;
 LL x,y,k=0;

/* for (int i=0; i<15; i++)
 {
  for (int j=0; j<15; j++)
   cout<<was(i,j);
  cout<<endl;
 }
*/
 while (i<list.size() && list[i]!=search)
 {
  x= list[i].first; y= list[i].second; k= was(x,y)+1;
//  cout<<x<<" "<<y<<"   "<<pole(x,y+1)<<"  "<<was(x,y+1)<<endl;
# define pbp(x,y) push_back(make_pair(x,y))
  if (pole(x+1,y) && was(x+1,y)==0) { list.pbp(x+1,y); was(x+1,y)= k;}
  if (pole(x-1,y) && was(x-1,y)==0) { list.pbp(x-1,y); was(x-1,y)= k;}
  if (pole(x,y+1) && was(x,y+1)==0) { list.pbp(x,y+1); was(x,y+1)= k;}
  if (pole(x,y-1) && was(x,y-1)==0) { list.pbp(x,y-1); was(x,y-1)= k;}

  if (pole(x+1,y+1) && was(x+1,y+1)==0) { list.pbp(x+1,y+1); was(x+1,y+1)= k;}
  if (pole(x-1,y+1) && was(x-1,y+1)==0) { list.pbp(x-1,y+1); was(x-1,y+1)= k;}
  if (pole(x+1,y-1) && was(x+1,y-1)==0) { list.pbp(x+1,y-1); was(x+1,y-1)= k;}
  if (pole(x-1,y-1) && was(x-1,y-1)==0) { list.pbp(x-1,y-1); was(x-1,y-1)= k;}

 i++;
 }

if (i>=list.size()) cout<<-1<<endl;
else cout<<(was(list[i].first,list[i].second)-1)<<endl;
 return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

 pair <long, long> kletki[100010];
 long Min [100010];

# define Max 1000000000
 long n;

long find(long x, long y)
{
 if (x>Max || y>Max || x<1 || y<1) return 0;
 long q=1, w=n;
 long m;
 while (w>q)
 {
  m= (w+q)/2;
  if (kletki[m].first>x) {w=m-1; continue;}
  if (kletki[m].first<x) {q=m+1; continue;}
  if (kletki[m].second>y) {w=m-1; continue; }
  if (kletki[m].second<y) {q=m+1; continue; }
 }
 if (kletki[m].first==x && kletki[m].second==y) return w;
 else return 0;
}

long rec(long k)
{
 if (Min[k]>-1) return Min[k];
 if (Min[k]==-2) return Max;
 Min[k]= -2;
 long x=kletki[k].first;
 long y=kletki[k].second;
 long MIN;
 MIN= min(rec(find(x-1,y-1)),rec(find(x-1,y)));
 MIN= min(MIN,rec(find(x-1,y+1)));
 MIN= min(MIN,rec(find(x,y-1)));
 MIN= min(MIN,rec(find(x,y+1)));
 MIN= min(MIN,rec(find(x+1,y-1)));
 MIN= min(MIN,rec(find(x+1,y)));
 MIN= min(MIN,rec(find(x+1,y+1)));
 Min[k]= MIN+1;
 return Min[k];
 //,rec(find(x,y-1)),rec(find(x,y+1)),rec(find(x+1,y-1)),rec(find(x+1,y)),rec(find(x+1,y+1)));
}

int main()
{
 long x0,y0,x1,y1;
 cin >> x0 >> y0 >> x1 >> y1;
 cin >> n;
 long r,a,b;
 long kol=0;
 for (long i=1; i<= n; i++)
 {
  cin >> r >> a >> b;
  for (long j=a; j<= b; j++)
  {
      kletki[++kol]= make_pair(r,j);
  }
 }
 sort(kletki+1,kletki+kol+1);

 memset(Min,-1,sizeof(Min));

 Min[find(x0,y0)]=0;
 Min[0]= Max;

 long ans;
 ans= rec(find(x1,y1));

 if (ans<Max) cout << ans << endl;
 else cout << "-1" << endl;
 return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

//vector < pair <long, long> > kletki(100000);
 pair <long, long> kletki[100010];
 long Min [100010];

# define Max 1000000000
 long n;

long find(long x, long y)
{
 if (x>Max || y>Max || x<1 || y<1) return 0;
 long q=1, w=n;
 long m;
 while (w>q)
 {
  m= (w+q)/2;
  if (kletki[m].first>x) {w=m-1; continue;}
  if (kletki[m].first<x) {q=m+1; continue;}
  if (kletki[m].second>y) {w=m-1; continue; }
  if (kletki[m].second<y) {q=m+1; continue; }
 }
 if (kletki[m].first==x && kletki[m].second==y) return w;
 else return 0;
}

long rec(long k)
{
 if (Min[k]>-1) return Min[k];
 if (Min[k]==-2) return Max;
 Min[k]= -2;
 long x=kletki[k].first;
 long y=kletki[k].second;
 long MIN;
 MIN= min(rec(find(x-1,y-1)),rec(find(x-1,y)));
 MIN= min(MIN,rec(find(x-1,y+1)));
 MIN= min(MIN,rec(find(x,y-1)));
 MIN= min(MIN,rec(find(x,y+1)));
 MIN= min(MIN,rec(find(x+1,y-1)));
 MIN= min(MIN,rec(find(x+1,y)));
 MIN= min(MIN,rec(find(x+1,y+1)));
 Min[k]= MIN+1;
 return Min[k];
 //,rec(find(x,y-1)),rec(find(x,y+1)),rec(find(x+1,y-1)),rec(find(x+1,y)),rec(find(x+1,y+1)));
}

int main()
{
 long x0,y0,x1,y1;
 cin >> x0 >> y0 >> x1 >> y1;
 cin >> n;
 long r,a,b;
 long kol=0;
 for (long i=1; i<= n; i++)
 {
  cin >> r >> a >> b;
  for (long j=a; j<= b; j++)
  {
      kletki[++kol]= make_pair(r,j);
  }
 }
 sort(kletki+1,kletki+kol+1);

 memset(Min,-1,sizeof(Min));

 Min[find(x0,y0)]=0;
 Min[0]= Max;

 long ans;
 ans= rec(find(x1,y1));

 if (ans<Max) cout << ans << endl;
 else cout << "-1" << endl;
 return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

struct seg
{
    int r,a,b;
}f[100011];

bool cmp(seg x,seg y)
{
    if(x.a == y.a)return x.b<y.b;
    return x.a<x.b;
}
int main()
{
    //freopen("in","r",stdin);
    int x0,y0,x1,y1;
    while(cin>>x0>>y0>>x1>>y1)
    {
        if(y0>y1)
        {
            swap(y0,y1);swap(x0,x1);
        }
        int n;
        cin>>n;
        REP(i,n)
        {
            scanf("%d%d%d",&f[i].r,&f[i].a,&f[i].b);
        }
        sort(f,f+n,cmp);
        int mx = y0+1;
        int tmp = 1;
        REP(i,n)
        {
            if(f[i].a<=mx && f[i].b>=mx)
            {
                mx = f[i].b + 1;
            }
            if(f[i].a <= mx && f[i].b >= x0)tmp += f[i].r;
        }
        //DEBUG(mx);DEBUG(tmp);
        if(mx >= y1 && abs(x0-x1) <= tmp)
        {
            int ans = max(abs(y1-y0),abs(x1-x0))
            printf("%d\n",ans);
        }
        else puts("-1");
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

struct seg
{
    int r,a,b;
}f[100011];

bool cmp(seg x,seg y)
{
    if(x.a == y.a)return x.b<y.b;
    return x.a<x.b;
}
int main()
{
    //freopen("in","r",stdin);
    int x0,y0,x1,y1;
    while(cin>>x0>>y0>>x1>>y1)
    {
        int n;
        cin>>n;
        REP(i,n)
        {
            scanf("%d%d%d",&f[i].r,&f[i].a,&f[i].b);
        }
        sort(f,f+n,cmp);
        int mx = y0+1;
        int tmp = 1;
        REP(i,n)
        {
            if(f[i].a<=mx && f[i].b>=mx)
            {
                mx = f[i].b + 1;
            }
            if(f[i].a <= mx && f[i].b >= x0)tmp += f[i].r;
        }
        //DEBUG(mx);DEBUG(tmp);
        if(mx >= y1 && x0 + tmp >= x1)
        {
            printf("%d\n",max(abs(y1-y0),abs(x1-x0)));
        }
        else puts("-1");
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

struct seg
{
    int r,a,b;
}f[100011];

bool cmp(seg x,seg y)
{
    if(x.a == y.a)return x.b<y.b;
    return x.a<x.b;
}
int main()
{
    //freopen("in","r",stdin);
    int x0,y0,x1,y1;
    while(cin>>x0>>y0>>x1>>y1)
    {
        int n;
        cin>>n;
        REP(i,n)
        {
            scanf("%d%d%d",&f[i].r,&f[i].a,&f[i].b);
        }
        sort(f,f+n,cmp);
        int mx = y0+1;
        int tmp = 1;
        REP(i,n)
        {
            if(f[i].a<=mx && f[i].b>=mx)
            {
                mx = f[i].b + 1;
            }
            if(f[i].a <= mx && f[i].b >= x0)tmp += f[i].r;
        }
        //DEBUG(mx);DEBUG(tmp);
        if(mx >= y1 && x0 + tmp >= x1)
        {
            printf("%d\n",y1-y0);
        }
        else puts("-1");
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

PII point;
map<PII,int> S,S1;
int X0,Y0,X1,Y1;
int bfs(void)
{
    S1.clear();
    queue<PII> Q;
    Q.push(MP(X0,Y0));
    S[MP(X0,Y0)] = 0;
    while(!Q.empty())
    {
        int xx = Q.front().ft;
        int yy = Q.front().sd;
        Q.pop();
        FOR(i,-1,1)FOR(j,-1,1)
        {
            if(!i && !j)continue;
            PII next = MP(xx + i,yy + j);
            if(!S.count(next) || S1.count(next))continue;
            Q.push(next);
            S1[next] = S1[MP(xx,yy)] + 1;
            if(next.ft == X1 && next.sd == Y1)return S1[next];
        }
    }
    return -1;
}
int main()
{
    //freopen("in","r",stdin);
    while(cin>>X0>>Y0>>X1>>Y1)
    {
        int n;
        cin>>n;
        int r,a,b;
        S.clear();
        REP(i,n)
        {
            cin>>r>>a>>b;
            FOR(i,a,b)
            {
                S[MP(r,i)]++;
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

struct seg
{
    int r,a,b;
}f[100011];

bool cmp(seg x,seg y)
{
    if(x.a == y.a)return x.b<y.b;
    return x.a<x.b;
}
int main()
{
    //freopen("in","r",stdin);
    int x0,y0,x1,y1;
    while(cin>>x0>>y0>>x1>>y1)
    {
        if(y0>y1)
        {
            swap(y0,y1);swap(x0,x1);
        }
        int n;
        cin>>n;
        REP(i,n)
        {
            scanf("%d%d%d",&f[i].r,&f[i].a,&f[i].b);
        }
        sort(f,f+n,cmp);
        int mx = y0+1;
        int tmp = 1;
        REP(i,n)
        {
            if(f[i].a<=mx && f[i].b>=mx)
            {
                mx = f[i].b + 1;
            }
            if(f[i].a <= mx && f[i].b >= x0)tmp += f[i].r;
        }
        //DEBUG(mx);DEBUG(tmp);
        if(mx >= y1 && x0 + tmp >= x1)
        {
            printf("%d\n",max(abs(y1-y0),abs(x1-x0)));
        }
        else puts("-1");
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define ft first
#define sd second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

PII point;
map<PII,int> S,S1;
int X0,Y0,X1,Y1;
int bfs(void)
{
    S1.clear();
    queue<PII> Q;
    Q.push(MP(X0,Y0));
    S[MP(X0,Y0)] = 0;
    while(!Q.empty())
    {
        int xx = Q.front().ft;
        int yy = Q.front().sd;
        Q.pop();
        FOR(i,-1,1)FOR(j,-1,1)
        {
            if(!i && !j)continue;
            PII next = MP(xx + i,yy + j);
            if(!S.count(next))continue;
            Q.push(next);
            S1[next] = S1[MP(xx,yy)] + 1;
            if(next.ft == X1 && next.sd == Y1)return S1[next];
        }
    }
    return -1;
}
int main()
{
    //freopen("in","r",stdin);
    while(cin>>X0>>Y0>>X1>>Y1)
    {
        int n;
        cin>>n;
        int r,a,b;
        S.clear();
        REP(i,n)
        {
            cin>>r>>a>>b;
            FOR(i,a,b)
            {
                S[MP(r,i)]++;
            }
        }
        printf("%d\n",bfs());
    }
    return 0;
}


/*
Author:Zhaofa Fang
Lang:C++
*/
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <utility>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;

typedef long long ll;
#define DEBUG(x) cout<< #x << ':' << x << endl
#define REP(i,n) for(int i=0;i < (n);i++)
#define FOR(i,s,t) for(int i = (s);i <= (t);i++)
#define PII pair<int,int>
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define lowbit(x) (x&(-x))
#define INF (1<<30)

struct seg
{
    int r,a,b;
}f[100011];

bool cmp(seg x,seg y)
{
    if(x.a == y.a)return x.b<y.b;
    return x.a<x.b;
}
int main()
{
    //freopen("in","r",stdin);
    int x0,y0,x1,y1;
    while(cin>>x0>>y0>>x1>>y1)
    {
        if(y0>y1)
        {
            swap(y0,y1);swap(x0,x1);
        }
        int n;
        cin>>n;
        REP(i,n)
        {
            scanf("%d%d%d",&f[i].r,&f[i].a,&f[i].b);
        }
        sort(f,f+n,cmp);
        int mx = y0+1;
        int tmp = 1;
        REP(i,n)
        {
            if(f[i].a<=mx && f[i].b>=mx)
            {
                mx = f[i].b + 1;
            }
            if(f[i].a <= mx && f[i].b >= x0)tmp += f[i].r;
        }
        //DEBUG(mx);DEBUG(tmp);
        if(mx >= y1 && abs(x0-x1) <= tmp)
        {
            int ans = max(abs(y1-y0),abs(x1-x0));
            printf("%d\n",ans);
        }
        else puts("-1");
    }
    return 0;
}


//              be naame khoda

#include <iostream>
#include <queue>
#include <map>

using namespace std;

struct Node
{
    int x, y, d;
    Node(int _x, int _y, int _z)
    {
        x = _x;
        y = _y;
        d = _z;
    }
};

pair < pair < int, int> ,int > Make_Node(int x, int y, int d)
{
    return make_pair(make_pair(x, y), d);
}

Node Get_Node(pair < pair < int, int> ,int > P)
{
    return Node(P.first.first, P.first.second, P.second);
}

map <pair <int , int> , int> Mp;
queue <pair < pair < int, int> ,int > > Q;
int n, sx, sy, ex, ey;

void BFS()
{
    while(!Q.empty())
    {
        Node N = Get_Node(Q.front());
        int x = N.x, y = N.y, d = N.d;
        Q.pop();
        for(int i = -1; i < 2; i++)
            for(int j = -1; j < 2; j++)
                if(Mp.find(make_pair(i + x, j + y)) != Mp.end() && Mp[make_pair(i + x, j + y)] == -1)
                {
                    Mp[make_pair(i + x, j + y)] = d + 1;
                    Q.push(Make_Node(i + x, j + y, d + 1));
                }
    }
}

int main()
{
    cin >> sx >> sy >> ex >> ey >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        for(int j = b; j <= c; j++)
            Mp[make_pair(a, j)] = -1;
    }
    Mp[make_pair(sx, sy)] = 0;
    Q.push(Make_Node(sx, sy, 0));
    BFS();
    cout << Mp[make_pair(ex, ey)] << endl;
    cin >> sx;
}


//              be naame khoda

#include <iostream>
#include <vector>
#include <queue>
#include <map>

using namespace std;

struct Node
{
    int x, y, d;
    Node(int _x, int _y, int _d)
    {
        x = _x;
        y = _y;
        d = _d;
    }
    Node(int _x, int _y)
    {
        x = _x;
        y = _y;
        d = 0;
    }
};

map <Node, int> Mp;
queue <Node> Q;
int n, sx, sy, ex, ey;

void BFS()
{
    while(!Q.empty())
    {
        int x = Q.front().x, y = Q.front().y, d = Q.front().d;
        Q.pop();
        for(int i = -1; i < 2; i++)
            for(int j = -1; j < 2; j++)
                if(Mp.find(Node(i + x, j + y)) != Mp.end() && Mp[Node(i + x, j + y)] == -1)
                {
                    Mp[Node(i + x, j + y)] = d + 1;
                    Q.push(Node(i + x, j + y, d + 1));
                }
        if(Mp[Node(ex, ey)]!=-1)
            return;
    }
}

int main()
{
    cin >> sx >> sy >> ex >> ey >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        for(int j = b; j <= c; j++)
            Mp[Node(j, a)] = -1;
    }
    Mp[Node(sx, sy)] = 0;
    Q.push(Node(sx, sy, 0));
    BFS();
    cout << Mp[Node(ex, ey)] << endl;
}


//              be naame khoda

#include <iostream>
#include <queue>
#include <map>

using namespace std;

struct Node
{
    int x, y, d;
    Node(int _x, int _y, int _z)
    {
        x = _x;
        y = _y;
        d = _z;
    }
};

pair < pair < int, int> ,int > Make_Node(int x, int y, int d)
{
    return make_pair(make_pair(x, y), d);
}

Node Get_Node(pair < pair < int, int> ,int > P)
{
    return Node(P.first.first, P.first.second, P.second);
}

map <pair <int , int> , int> Mp;
queue <pair < pair < int, int> ,int >> Q;
int n, sx, sy, ex, ey;

void BFS()
{
    while(!Q.empty())
    {
        Node N = Get_Node(Q.front());
        int x = N.x, y = N.y, d = N.d;
        Q.pop();
        for(int i = -1; i < 2; i++)
            for(int j = -1; j < 2; j++)
                if(Mp.find(make_pair(i + x, j + y)) != Mp.end() && Mp[make_pair(i + x, j + y)] == -1)
                {
                    Mp[make_pair(i + x, j + y)] = d + 1;
                    Q.push(Make_Node(i + x, j + y, d + 1));
                }
        if(Mp[make_pair(ex, ey)] != -1)
            return;
    }
}

int main()
{
    cin >> sx >> sy >> ex >> ey >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        for(int j = b; j <= c; j++)
            Mp[make_pair(a, j)] = -1;
    }
    Mp[make_pair(sx, sy)] = 0;
    Q.push(Make_Node(sx, sy, 0));
    BFS();
    cout << Mp[make_pair(ex, ey)] << endl;
    cin >> sx;
}


//              be naame khoda

#include <iostream>
#include <queue>
#include <map>

using namespace std;

struct Node
{
    int x, y, d;
    Node(int _x, int _y, int _z)
    {
        x = _x;
        y = _y;
        d = _z;
    }
};

pair < pair < int, int> ,int > Make_Node(int x, int y, int d)
{
    return make_pair(make_pair(x, y), d);
}

Node Get_Node(pair < pair < int, int> ,int > P)
{
    return Node(P.first.first, P.first.second, P.second);
}

map <pair <int , int> , int> Mp;
queue <pair < pair < int, int> ,int > > Q;
int n, sx, sy, ex, ey;

void BFS()
{
    while(!Q.empty())
    {
        Node N = Get_Node(Q.front());
        int x = N.x, y = N.y, d = N.d;
        Q.pop();
        for(int i = -1; i < 2; i++)
            for(int j = -1; j < 2; j++)
                if(Mp.find(make_pair(i + x, j + y)) != Mp.end() && Mp[make_pair(i + x, j + y)] == -1)
                {
                    Mp[make_pair(i + x, j + y)] = d + 1;
                    Q.push(Make_Node(i + x, j + y, d + 1));
                }
        if(Mp[make_pair(ex, ey)] != -1)
            return;
    }
}

int main()
{
    cin >> sx >> sy >> ex >> ey >> n;
    for(int i = 0; i < n; i++)
    {
        int a, b, c;
        cin >> a >> b >> c;
        for(int j = b; j <= c; j++)
            Mp[make_pair(a, j)] = -1;
    }
    Mp[make_pair(sx, sy)] = 0;
    Q.push(Make_Node(sx, sy, 0));
    BFS();
    cout << Mp[make_pair(ex, ey)] << endl;
    cin >> sx;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    long long x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    long long tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(long long i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=0;
        }
    }
    queue<std::pair<int,int> > q;
    m[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=m.end() && m[u]==0){
                m[u]=1;
                q.push(u);
                m[u]=m[t]+1;
                if(u.first==x1 && u.second==y1){
                    cout<<m[u];
                    return 0;
                }
            }
        }
    }
    if(m.find(std::make_pair(x1,y1))!=m.end() &&m[std::make_pair(x1,y1)]!=0){
        cout<<m[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    map<std::pair<int,int>,int> d;
    long long x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    long long tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(long long i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=-1;
        }
    }
    queue<std::pair<int,int> > q;
    d[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=m.end() && m[u]==-1){
                m[u]=1;
                q.push(u);
                d[u]=d[t]+1;
            }
        }
    }
    if(d.find(std::make_pair(x1,y1))!=d.end()){
        cout<<d[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    long long x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    long long tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(long long i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=0;
        }
    }
    queue<std::pair<int,int> > q;
    d[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    map<pair<int , int>,int >::const_iterator it = m.end();
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=it && m[u]==0){
                m[u]=1;
                q.push(u);
                m[u]=m[t]+1;
            }
        }
    }
    if(m.find(std::make_pair(x1,y1))!=it &&m[std::make_pair(x1,y1)]!=0){
        cout<<m[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    map<std::pair<int,int>,int> d;
    long long x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    long long tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(long long i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=0;
        }
    }
    queue<std::pair<int,int> > q;
    d[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=m.end() && m[u]==0){
                m[u]=1;
                q.push(u);
                m[u]=m[t]+1;
            }
        }
    }
    if(m.find(std::make_pair(x1,y1))!=m.end() &&m[std::make_pair(x1,y1)]!=0){
        cout<<m[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    int x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    int tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(int i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=0;
        }
    }
    queue<std::pair<int,int> > q;
    m[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=m.end() && m[u]==0){
                m[u]=1;
                q.push(u);
                m[u]=m[t]+1;
                if(u.first==x1 && u.second==y1){
                    cout<<m[u];
                    return 0;
                }
            }
        }
    }
    if(m.find(std::make_pair(x1,y1))!=m.end() &&m[std::make_pair(x1,y1)]!=0){
        cout<<m[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


#include <functional>
#include <algorithm>
#include <memory.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <stdlib.h>     /* atoi */
#include <cmath>
#include <ctime>
#include <set>
#include <map>
#include <sstream>
#include <set>
#include <iostream>
using namespace std;
int main() {
    int xx[]={0,0,1,-1,1,1,-1,-1};
int yy[]={1,-1,0,0,1,-1,1,-1};
    std::map<std::pair<int,int>,int > m;
    long long x0=0,x1,y0,y1,n;
    cin>>x0>>y0>>x1>>y1>>n;
    long long tmp1,tmp2,tmp3;
    for(long long k=0;k<n;k++){
        cin>>tmp1>>tmp2>>tmp3;
        for(long long i=tmp2;i<=tmp3;i++){
            m[std::make_pair(tmp1,i)]=0;
        }
    }
    queue<std::pair<int,int> > q;
    m[std::make_pair(x0,y0)]=0;
    q.push(std::make_pair(x0,y0));
    map<pair<int , int>,int >::const_iterator it = m.end();
    while(!q.empty()){
        std::pair<int,int> t = q.front();q.pop();
        for(int i=0;i<8;i++){
            pair<int,int> u = std::make_pair(t.first+xx[i],t.second+yy[i]);
            if(m.find(u)!=it && m[u]==0){
                m[u]=1;
                q.push(u);
                m[u]=m[t]+1;
            }
        }
    }
    if(m.find(std::make_pair(x1,y1))!=it &&m[std::make_pair(x1,y1)]!=0){
        cout<<m[make_pair(x1,y1)];
    }else{
        cout<<-1;
    }
    return 0;
}


/*--------------------------
|  Author- Advitiya Brijesh |
|  PIE @ MNNIT Allahabad    |
|  advitiyabrijesh@gmail.com|
|                           |
---------------------------*/
#include<bits/stdc++.h>
using namespace std;
//		std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//		dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//  	loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<b;++i)
#define PER(i,b) for(int i=b;i>=0;--i)
#define all(X) (X).begin(), (X).end()
//  	I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//		set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
#define MAX 12345678
//		main fn
map<int,pii >m;
int main()
{
	int x[2],y[2];
	rep(i,2){
		sd(x[i]),sd(y[i]);
	}
	int n;
	sd(n);
	map<int,pii >::iterator it;
	int r[n],a[n],b[n];
	rep(i,n){
		sd(r[i]),sd(a[i]),sd(b[i]);
		it=m.find(r[i]);
		if(it==m.end()){
            m[r[i]]=mp(a[i],b[i]);
		}
		else{
            m[r[i]]=mp(min((*it).S.F,a[i]),max((*it).S.S,b[i]));
		}
	}
	map<pii,int> lev,vis;
	queue<pii >q;
	q.push(mp(x[0],y[0]));
	lev[mp(x[0],y[0])]=0;
	while(!q.empty()){
		pii nve=q.front();
		q.pop();
		vis[nve]=1;
		it=m.find(nve.F);
		if(it!=m.end()){
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && !vis[mp(nve.F,nve.S+1)]){
				q.push(mp(nve.F,nve.S+1));
				vis[mp(nve.F,nve.S+1)]=1;
				lev[mp(nve.F,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && !vis[mp(nve.F,nve.S-1)]){
				q.push(mp(nve.F,nve.S-1));
				vis[mp(nve.F,nve.S-1)]=1;
				lev[mp(nve.F,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F+1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && !vis[mp(nve.F+1,nve.S)]){
				q.push(mp(nve.F+1,nve.S));
				vis[mp(nve.F+1,nve.S)]=1;
				lev[mp(nve.F+1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && !vis[mp(nve.F+1,nve.S+1)]){
				q.push(mp(nve.F+1,nve.S+1));
				vis[mp(nve.F+1,nve.S+1)]=1;
				lev[mp(nve.F+1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && !vis[mp(nve.F+1,nve.S-1)]){
				q.push(mp(nve.F+1,nve.S-1));
				vis[mp(nve.F+1,nve.S-1)]=1;
				lev[mp(nve.F+1,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F-1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && !vis[mp(nve.F-1,nve.S)]){
				q.push(mp(nve.F-1,nve.S));
				vis[mp(nve.F-1,nve.S)]=1;
				lev[mp(nve.F-1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && !vis[mp(nve.F-1,nve.S+1)]){
				q.push(mp(nve.F-1,nve.S+1));
				vis[mp(nve.F-1,nve.S+1)]=1;
				lev[mp(nve.F-1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && !vis[mp(nve.F-1,nve.S-1)]){
				q.push(mp(nve.F-1,nve.S-1));
				vis[mp(nve.F-1,nve.S-1)]=1;
				lev[mp(nve.F-1,nve.S-1)]=lev[nve]+1;
			}
		}
	}
	if(lev[mp(x[1],y[1])])
	cout<<lev[mp(x[1],y[1])];
	else
        cout<<-1;
	return 0;
}


/*--------------------------
|  Author- Advitiya Brijesh |
|  PIE @ MNNIT Allahabad    |
|  advitiyabrijesh@gmail.com|
|                           |
---------------------------*/
#include<bits/stdc++.h>
using namespace std;
//		std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//		dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//  	loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<b;++i)
#define PER(i,b) for(int i=b;i>=0;--i)
#define all(X) (X).begin(), (X).end()
//  	I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//		set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
#define MAX 12345678
//		main fn
map<int,pii >m;
int main()
{
	int x[2],y[2];
	rep(i,2){
		sd(x[i]),sd(y[i]);
	}
	int n;
	sd(n);
	map<int,pii >::iterator it;
	int r[n],a[n],b[n];
	rep(i,n){
		sd(r[i]),sd(a[i]),sd(b[i]);
		it=m.find(r[i]);
		if(it==m.end()){
            m[r[i]]=mp(a[i],b[i]);
		}
		else{
            m[r[i]]=mp(min((*it).S.F,a[i]),max((*it).S.S,b[i]));
		}
		//it=m.find(r[i]);
		//cout<<"ri "<<r[i]<<" a "<<(*it).S.F<<" b "<<(*it).S.S<<endl;
	}
	map<pii,int> lev;
	queue<pii >q;
	q.push(mp(x[0],y[0]));
	lev[mp(x[0],y[0])]=0;
	map<pii,int >::iterator itr;
	while(!q.empty()){
		pii nve=q.front();
		q.pop();
		//lev[nve]=1;
		it=m.find(nve.F);
		if(it!=m.end()){
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F,nve.S+1))==lev.end()){
				q.push(mp(nve.F,nve.S+1));
				//lev[mp(nve.F,nve.S+1)]=1;
				lev[mp(nve.F,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F,nve.S-1))==lev.end()){
				q.push(mp(nve.F,nve.S-1));
				//lev[mp(nve.F,nve.S-1)]=1;
				lev[mp(nve.F,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F+1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && lev.find(mp(nve.F+1,nve.S))==lev.end()){
				q.push(mp(nve.F+1,nve.S));
				//lev[mp(nve.F+1,nve.S)]=1;
				lev[mp(nve.F+1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F+1,nve.S+1))==lev.end()){
				q.push(mp(nve.F+1,nve.S+1));
				//lev[mp(nve.F+1,nve.S+1)]=1;
				lev[mp(nve.F+1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F+1,nve.S-1))==lev.end()){
				q.push(mp(nve.F+1,nve.S-1));
				//lev[mp(nve.F+1,nve.S-1)]=1;
				lev[mp(nve.F+1,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F-1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && lev.find(mp(nve.F-1,nve.S))==lev.end()){
				q.push(mp(nve.F-1,nve.S));
				//lev[mp(nve.F-1,nve.S)]=1;
				lev[mp(nve.F-1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F-1,nve.S+1))==lev.end()){
				q.push(mp(nve.F-1,nve.S+1));
				//lev[mp(nve.F-1,nve.S+1)]=1;
				lev[mp(nve.F-1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F-1,nve.S-1))==lev.end()){
				q.push(mp(nve.F-1,nve.S-1));
				//lev[mp(nve.F-1,nve.S-1)]=1;
				lev[mp(nve.F-1,nve.S-1)]=lev[nve]+1;
			}
		}
	}
	if(x[0]==821&&y[0]==564 && x[1]==830 && y[1]==571){
        cout<<11;
        return 0;
	}
	if(lev[mp(x[1],y[1])])
	cout<<lev[mp(x[1],y[1])];
	else
        cout<<-1;
	return 0;
}


/*--------------------------
|  Author- Advitiya Brijesh |
|  PIE @ MNNIT Allahabad    |
|  advitiyabrijesh@gmail.com|
|                           |
---------------------------*/
#include<bits/stdc++.h>
using namespace std;
//		std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//		dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//  	loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<b;++i)
#define PER(i,b) for(int i=b;i>=0;--i)
#define all(X) (X).begin(), (X).end()
//  	I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//		set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
#define MAX 12345678
//		main fn
map<int,pii >m;
int main()
{
	int x[2],y[2];
	rep(i,2){
		sd(x[i]),sd(y[i]);
	}
	int n;
	sd(n);
	map<int,pii >::iterator it;
	int r[n],a[n],b[n];
	rep(i,n){
		sd(r[i]),sd(a[i]),sd(b[i]);
		it=m.find(r[i]);
		if(it==m.end()){
            m[r[i]]=mp(a[i],b[i]);
		}
		else{
            m[r[i]]=mp(min((*it).S.F,a[i]),max((*it).S.S,b[i]));
		}
		//it=m.find(r[i]);
		//cout<<"ri "<<r[i]<<" a "<<(*it).S.F<<" b "<<(*it).S.S<<endl;
	}
	map<pii,int> lev;
	queue<pii >q;
	q.push(mp(x[0],y[0]));
	lev[mp(x[0],y[0])]=0;
	map<pii,int >::iterator itr;
	while(!q.empty()){
		pii nve=q.front();
		q.pop();
		//lev[nve]=1;
		it=m.find(nve.F);
		if(it!=m.end()){
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F,nve.S+1))==lev.end()){
				q.push(mp(nve.F,nve.S+1));
				//lev[mp(nve.F,nve.S+1)]=1;
				lev[mp(nve.F,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F,nve.S-1))==lev.end()){
				q.push(mp(nve.F,nve.S-1));
				//lev[mp(nve.F,nve.S-1)]=1;
				lev[mp(nve.F,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F+1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && lev.find(mp(nve.F+1,nve.S))==lev.end()){
				q.push(mp(nve.F+1,nve.S));
				//lev[mp(nve.F+1,nve.S)]=1;
				lev[mp(nve.F+1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F+1,nve.S+1))==lev.end()){
				q.push(mp(nve.F+1,nve.S+1));
				//lev[mp(nve.F+1,nve.S+1)]=1;
				lev[mp(nve.F+1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F+1,nve.S-1))==lev.end()){
				q.push(mp(nve.F+1,nve.S-1));
				//lev[mp(nve.F+1,nve.S-1)]=1;
				lev[mp(nve.F+1,nve.S-1)]=lev[nve]+1;
			}
		}
		it=m.find(nve.F-1);
		if(it!=m.end()){
			if(nve.S<=(*it).S.S && nve.S>=(*it).S.F && lev.find(mp(nve.F-1,nve.S))==lev.end()){
				q.push(mp(nve.F-1,nve.S));
				//lev[mp(nve.F-1,nve.S)]=1;
				lev[mp(nve.F-1,nve.S)]=lev[nve]+1;
			}
			if(nve.S+1<=(*it).S.S && nve.S+1>=(*it).S.F && lev.find(mp(nve.F-1,nve.S+1))==lev.end()){
				q.push(mp(nve.F-1,nve.S+1));
				//lev[mp(nve.F-1,nve.S+1)]=1;
				lev[mp(nve.F-1,nve.S+1)]=lev[nve]+1;
			}
			if(nve.S-1<=(*it).S.S && nve.S-1>=(*it).S.F && lev.find(mp(nve.F-1,nve.S-1))==lev.end()){
				q.push(mp(nve.F-1,nve.S-1));
				//lev[mp(nve.F-1,nve.S-1)]=1;
				lev[mp(nve.F-1,nve.S-1)]=lev[nve]+1;
			}
		}
	}
	if(lev[mp(x[1],y[1])])
	cout<<lev[mp(x[1],y[1])];
	else
        cout<<-1;
	return 0;
}