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

import java.io.*;
import java.util.*;
public final class kings_path
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static int maxn=(int)(2e5)+3;
	static ArrayList<Node>[] al;
	static ArrayList<Integer>[] al2;
	static List<Node> list;
	
	static int search(int x,int y)
	{
		return Collections.binarySearch(list,new Node(x,y));
	}
	
	@SuppressWarnings("unchecked")
	public static void main(String args[]) throws Exception
	{
		int x1=sc.nextInt(),y1=sc.nextInt(),x2=sc.nextInt(),y2=sc.nextInt(),n=sc.nextInt();int[] a=new int[n];Triplet[] b=new Triplet[n];al=new ArrayList[n];
		for(int i=0;i<n;i++)
		{
			b[i]=new Triplet(sc.nextInt(),sc.nextInt(),sc.nextInt());a[i]=b[i].x;al[i]=new ArrayList<Node>();
		}
		Arrays.sort(a);boolean[] v=new boolean[maxn];List<Integer> now=new ArrayList<Integer>();
		for(int i=0;i<n;i++)
		{
			int pos=Arrays.binarySearch(a,b[i].x);al[pos].add(new Node(b[i].y,b[i].z));
			if(!v[pos])
			{
				v[pos]=true;now.add(pos);
			}
		}
		list=new ArrayList<Node>();
		for(int i=0;i<now.size();i++)
		{
			int curr=now.get(i);Collections.sort(al[curr]);int last=0,map=a[curr];
			for(int j=0;j<al[curr].size();j++)
			{
				int s=Math.max(al[curr].get(j).x,last+1);
				for(int k=s;k<=al[curr].get(j).y;k++)
				{
					list.add(new Node(map,k));
				}
				last=Math.max(last,al[curr].get(j).y);
			}
		}
		/*
		for(Node x:list)
		{
			out.println(x.x+" "+x.y);
		}
		*/
		al2=new ArrayList[list.size()+1];Collections.sort(list);
		for(int i=0;i<list.size();i++)
		{
			al2[i]=new ArrayList<Integer>();
		}
		for(int i=0;i<list.size();i++)
		{
			int[] d=new int[8];int x=list.get(i).x,y=list.get(i).y;d[0]=search(x,y+1);d[1]=search(x,y-1);d[2]=search(x+1,y);d[3]=search(x-1,y);d[4]=search(x-1,y-1);d[5]=search(x+1,y+1);d[6]=search(x-1,y+1);d[7]=search(x+1,y-1);
			for(int j=0;j<8;j++)
			{
				if(d[j]>=0)
				{
					al2[i].add(d[j]);
				}
			}
		}
		ArrayDeque<Node> pq=new ArrayDeque<Node>();int curr_pos=search(x1,y1);pq.add(new Node(curr_pos,0));a=new int[list.size()];Arrays.fill(a,Integer.MAX_VALUE);a[curr_pos]=0;
		while(pq.size()>0)
		{
			Node curr=pq.removeFirst();
			for(int x:al2[curr.x])
			{
				if(a[x]>a[curr.x]+1)
				{
					a[x]=a[curr.x]+1;pq.add(new Node(x,a[x]));
				}
			}
		}
		int f_pos=search(x2,y2);out.println(a[f_pos]<Integer.MAX_VALUE?a[f_pos]:-1);out.close();;
	}
}
class Node implements Comparable<Node>
{
	int x,y;
	public Node(int x,int y)
	{
		this.x=x;this.y=y;
	}
	public int compareTo(Node x1)
	{
		if(this.x==x1.x)
		{
			return Integer.compare(this.y,x1.y);
		}
		return Integer.compare(this.x,x1.x);
	}
}
class Triplet
{
	int x,y,z;
	public Triplet(int x,int y,int z)
	{
		this.x=x;this.y=y;this.z=z;
	}
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}