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

#include <bits/stdc++.h>
using namespace std;
int main() {
  queue<pair<int, int> > bfs;
  queue<int> cekori;
  pair<int, int> start, kraj;
  map<pair<int, int>, int> mapa;
  map<pair<int, int>, int>::iterator it;
  int n, i, j, one, two, three;
  cin >> start.first >> start.second >> kraj.first >> kraj.second;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> one >> two >> three;
    for (j = two; j <= three; j++) {
      pair<int, int> temp = make_pair(one, j);
      mapa[temp] = 1000000000;
    }
  }
  mapa[start] = 0;
  bfs.push(start);
  cekori.push(0);
  while (!bfs.empty()) {
    pair<int, int> temp = bfs.front();
    int cek = cekori.front();
    bfs.pop();
    cekori.pop();
    pair<int, int> poteg = make_pair(temp.first - 1, temp.second);
    if ((temp.first - 1 >= 0) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first + 1, temp.second);
    if ((temp.first + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first, temp.second - 1);
    if ((temp.second - 1 >= 0) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first, temp.second + 1);
    if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first - 1, temp.second + 1);
    if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first + 1, temp.second + 1);
    if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first + 1, temp.second - 1);
    if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    poteg = make_pair(temp.first - 1, temp.second - 1);
    if ((temp.second + 1 <= 1000000000) && (mapa.find(poteg) != mapa.end()) &&
        (mapa[poteg] > cek + 1)) {
      mapa[poteg] = cek + 1;
      bfs.push(poteg);
      cekori.push(cek + 1);
    }
    if (mapa[kraj] < 1000000000) {
      cout << mapa[kraj];
      return 0;
    }
  }
  cout << mapa[kraj];
  return 0;
}
