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 class nC {

    static class Assert {
        static void check(boolean e) {
            if (!e) {
                throw new Error();
            }
        }
    }

    static class Scanner {
        InputStream in;
        StringBuilder sb = new StringBuilder(16);
        byte buffer[] = new byte[1 << 16];
        int len = 0;
        int pos = 0;

        Scanner(InputStream is) {
            in = is;
        }

        int nextChar() {
            try {
                if (pos == len) {
                    len = Math.max(in.read(buffer), 0);
                    pos = 0;
                }
                if (len == 0) {
                    return -1;
                }
                Assert.check(pos < len);
                int value = buffer[pos] & 0xFF;
                pos++;
                return value;
            } catch (IOException e) {
                throw new Error(e);
            }
        }

        StringBuilder next() {
            sb.setLength(0);
            int ch = nextChar();
            while (ch == ' ' || ch == '\r' || ch == '\n') {
                ch = nextChar();
            }
            Assert.check('A' <= ch && ch <= 'Z');
            while (true) {
                if ('A' <= ch && ch <= 'Z') {
                    sb.append((char) ch);
                } else if (ch == ' ' || ch == '\r' || ch == '\n' || ch == -1) {
                    break;
                } else {
                    throw new Error();
                }
                ch = nextChar();
            }
            return sb;
        }

        int nextInt() {
            int ch = nextChar();
            while (ch == ' ' || ch == '\r' || ch == '\n') {
                ch = nextChar();
            }
            int sign = 1;
            if (ch == '-') {
                sign = -1;
                ch = nextChar();
            }
            Assert.check('0' <= ch && ch <= '9');
            int value = ch - '0';
            ch = nextChar();
            Assert.check('0' <= ch && ch <= '9' || ch == ' ' || ch == '\r'
                    || ch == '\n' || ch == -1);
            while ('0' <= ch && ch <= '9') {
                Assert.check(value <= Integer.MAX_VALUE / 10);
                value *= 10;
                value += ch - '0';
                Assert.check(value > 0);
                ch = nextChar();
            }
            Assert.check(ch == ' ' || ch == '\r' || ch == '\n' || ch == -1);
            return sign * value;
        }
    }

    Scanner in;
    PrintWriter out;

    void swap(int x, int y) {
        int buf = x;
        x = y;
        y = buf;
    }

    class Stripe {
        int l;
        int r;
    }

    void solve() {
        int xst = in.nextInt();
        int yst = in.nextInt();
        int xfin = in.nextInt();
        int yfin = in.nextInt();
        int y0 = Math.min(yst, yfin);
        int y1 = Math.max(yst, yfin);
        int x0 = Math.min(xst, xfin);
        int x1 = Math.max(xst, xfin);
        int n = in.nextInt();
        Stripe arr[] = new Stripe[x1 - x0 + 1];
        for (int i = 0; i < (x1 - x0 + 1); i++) {
            arr[i] = new Stripe();
            arr[i].l = y1;
            arr[i].r = y0;
        }
        for (int i = 0; i < n; i++) {
            int r = in.nextInt();
            int a = in.nextInt();
            int b = in.nextInt();
            if (x0 <= r && r <= x1) {
                r -= x0;
                if (a <= arr[r].l && b >= y0) {
                    arr[r].l = Math.min(a, arr[r].l);
                    arr[r].l = Math.max(arr[r].l, y0);
                }
                if (a <= arr[r].r && b >= y0) {
                    arr[r].r = Math.max(arr[r].r, b);
                    arr[r].r = Math.min(arr[r].r, y1);
                }
            }
        }
        if (Math.sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1)) > 100000) {
            out.println(-1);
            return;
        }
        int rez = 0;
        for (int i = 0; i < (x1 - x0); i++) {
            if (arr[i + 1].l > arr[i].r) {
                out.println(-1);
                return;
            }
            int minUp = arr[i + 1].l - arr[i].l;
            rez += minUp;
        }
        out.println(rez + y1 - y0);
    }

    void run() {
        in = new Scanner(System.in);
        out = new PrintWriter(System.out);

        try {
            solve();
        } finally {
            out.close();
        }
    }

    public static void main(String[] args) {
        new nC().run();
    }
}
