1 1 1 2
5
1000000000 1 10000
19920401 1188 5566
1000000000 1 10000
1 1 10000
5 100 200
 6 15 7 15
9
6 15 15
7 14 14
6 15 15
9 14 14
7 14 16
6 15 15
6 15 15
7 14 14
8 15 15
 1 1 1000000000 2
5
1000000000 1 10000
19920401 1188 5566
1000000000 1 10000
1 1 10000
5 100 200
 89 29 88 30
16
87 31 31
14 95 95
98 88 89
96 88 88
14 97 97
13 97 98
100 88 88
88 32 32
99 88 89
90 29 29
87 31 31
15 94 96
89 29 29
88 32 32
97 89 89
88 29 30
 30 14 39 19
31
35 7 11
37 11 12
32 13 13
37 5 6
46 13 13
37 14 14
31 13 13
43 13 19
45 15 19
46 13 13
32 17 17
41 14 19
30 14 14
43 13 17
34 16 18
44 11 19
38 13 13
40 12 20
37 16 18
46 16 18
34 10 14
36 9 10
36 15 19
38 15 19
42 13 19
33 14 15
35 15 19
33 17 18
39 12 20
36 5 7
45 12 12
 9 8 7 8
9
10 6 6
10 6 6
7 7 8
9 5 6
8 9 9
9 5 5
9 8 8
8 5 6
9 10 10
 2 1 1 1
2
1 1 2
2 1 2
 13 16 20 10
18
13 16 16
20 10 10
19 10 10
12 15 15
20 10 10
18 11 11
19 10 10
19 10 10
20 10 10
19 10 10
20 10 10
20 10 10
19 10 10
18 11 11
13 16 16
12 15 15
19 10 10
19 10 10

1
 1
 -1
 1
 9
 2
 1
 -1

// abhishek soni.....[NSIT'22]
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define letsgo                  ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                    '\n'
#define mp                      make_pair
#define pb                      push_back
#define pf                      push_front
#define pob                     pop_back
#define pof                     pop_front
#define ff                      first
#define ss                      second
#define ALL(x)                  (x).begin(),(x).end()
#define int                     long long
#define ll                      long long
#define deb(x,y)                cout<<x<<" == "<<y<<endl;
#define _deb(x)                    cout<<"# "<<x<<endl;
#define set_bits(x)              __builtin_popcountll(x)
#define unset_bits(x)              __builtin_ctzll(x)
#define nl(i,a,b,j,m,n)         for(int i=a;i<b;i++)for(int j=m;j<n;j++)
#define fl(i,a,b)               for(int i=a;i<b;i++)
#define bl(i,a,b)               for(int i=a;i>=b;i--)
#define ma(arr,size,type)       type*arr=new type[size]()
#define m2a(arr,row,col,type)   type**arr=new type*[row];fl(i,0,row) arr[i]=new type[col]()
using namespace std;
using namespace __gnu_pbds;
typedef pair<int,int>           pii;
typedef pair<int,pair<int,int>> piii;
typedef vector<vector<int>>     matrix;
/*______________________________________________________________________________________________________________*/
int pow(int a,int b,int m){
    if(a==0)return 0;
    if(b==0)return 1;
    int temp=pow(a,b/2,m);
    temp = ((temp%m)*(temp%m))%m;
    if(b&1) temp = ((temp%m)*(a%m))%m;
    return temp;
}
int string_to_num(string s){
    stringstream num(s); int x; num >> x; return x;
}
string num_to_string(int n){
    string s;
    while(n>0){ s=char(n%10+'0')+s; n/=10; }
    return ((s.length()==0)? "0" : s );
}
/*______________________________________________________________________________________________________________*/

const int mod=1e9+7;
const int R_max=1e2+5;
const int C_max=1e2+5;
/*______________________________________________________________________________________________________________*/
bool comp(pii a,pii b){
    if(a.ff != b.ff){
        return a.ss > b.ss;
    }else{
        return a.ff < b.ff;
    }
}
void solve(){
    int x0,y0,x1,y1; cin>>x0>>y0>>x1>>y1;
    int n; cin>>n;
    ma(arrx,n,int);
    vector<pii> arry(n);
    fl(i,0,n){
        cin>>arrx[i]>>arry[i].ff>>arry[i].ss;
    }
    int ans = max(abs(y1 - y0),abs(x0 - x1));

    sort(arrx,arrx+n);
    sort(ALL(arry),comp);
    if(x1 - x0 > n){
        cout<<-1<<endl;
        return;
    }
    // check x
    int l_l = (lower_bound(arrx,arrx+n,x0) - arrx);
    // cout<<"l_l : "<<l_l<<endl;
    bool ff = true;
    if( !(arrx[l_l] == x0 || arrx[l_l] == x0+1 )) ff = false;
    int curr = arrx[l_l];
    for(int i=l_l;i<n;){
        if(arrx[i] != curr){
            i++;
            continue;
        }
        i++;
        curr++;
    }
    if(x1 >= curr) ff = false;
    // cout<<"curr : "<<curr<<endl;
    if(!ff){
        cout<<-1<<endl;
        return;
    }


    // check y

    y0++;
    fl(i,0,n){
        if(arry[i].ff <= y0 && arry[i].ss >= y0){
            y0 = max(y0,arry[i].ss + 1 );
        }
    }
    // cout<<"y0 : "<<y0<<endl;
    if(y0 < y1) ff = false;
    if(!ff){
        cout<<-1<<endl;
        return;
    }
    cout<<ans<<endl;

    
}

signed main(){
    letsgo
    int t=1;
    // cin>>t;
    for(int T=1;T<=t;T++){
        // cout<<"test case # "<<T<<endl;
        // clock_t z = clock();
        solve();
        // cerr << "Run Time : " << ((double)(clock() - z) / CLOCKS_PER_SEC);
    }
}