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>
struct Segment {
  Segment(int x_, int y_) : x(x_), y(y_), visited(0) {}
  int x;
  int y;
  char visited;
  long long val(void) { return y << 32 + x; }
};
const int MSIZE = 100000;
Segment* slist[MSIZE];
int segments;
template <typename T>
void swap(T& v1, T& v2) {
  T tmp = v1;
  v1 = v2;
  v2 = tmp;
}
template <typename T, int MSIZE>
class Heap {
 public:
  Heap() : m_size(0) {}
  int size(void) { return m_size; }
  void push(T v) {
    m_data[m_size++] = v;
    bubbleUp(m_size - 1);
  }
  T pop(void) {
    T tmp = m_data[0];
    m_data[0] = m_data[--m_size];
    bubbleDown(0);
    return tmp;
  }

 private:
  void bubbleUp(int c) {
    if (c == 0) return;
    int p = (c - 1) / 2;
    if (m_data[c]->val() < m_data[p]->val()) {
      swap(m_data[c], m_data[p]);
      bubbleUp(p);
    }
  }
  void bubbleDown(int p) {
    int l = p * 2 + 1;
    int r = p * 2 + 2;
    int m = p;
    if (l < m_size && m_data[l]->val() < m_data[m]->val()) m = l;
    if (r < m_size && m_data[r]->val() < m_data[m]->val()) m = r;
    if (m != p) {
      swap(m_data[p], m_data[m]);
      bubbleDown(m);
    }
  }

 private:
  T m_data[MSIZE];
  int m_size;
};
struct Point {
  Point(void) : x(-1), y(-1), dist(0) {}
  Point(int x_, int y_) : x(x_), y(y_), dist(0) {}
  int x;
  int y;
  int dist;
};
int N;
Point queue[MSIZE];
int queueRead;
int queueWrite;
void addTask(const Point& p, int dist) {
  int l = 0;
  int r = segments - 1;
  long long v = p.y << 32 + p.x;
  int sid = -1;
  while (l <= r) {
    int m = (l + r) / 2;
    if (slist[m]->val() == v) {
      sid = m;
      break;
    } else if (v > slist[m]->val())
      l = m + 1;
    else
      r = m - 1;
  }
  if (sid == -1) return;
  if (slist[sid]->visited == 1) return;
  slist[sid]->visited = 1;
  queue[queueWrite++] = p;
  queue[queueWrite - 1].dist = dist + 1;
}
int main(void) {
  int X0, Y0, X1, Y1;
  scanf("%d %d %d %d\n", &Y0, &X0, &Y1, &X1);
  scanf("%d\n", &N);
  Heap<Segment*, MSIZE> heap;
  for (int n = 0; n < N; ++n) {
    int R, A, B;
    scanf("%d %d %d\n", &R, &A, &B);
    for (int x = A; x <= B; ++x) heap.push(new Segment(x, R));
  }
  segments = heap.size();
  int pos = 0;
  while (heap.size() > 0) {
    slist[pos] = heap.pop();
    pos++;
  }
  addTask(Point(X0, Y0), 0);
  bool found = false;
  int dist = 0;
  while (queueRead < queueWrite) {
    Point p = queue[queueRead++];
    if (p.x == X1 && p.y == Y1) {
      dist = p.dist;
      found = true;
      break;
    }
    addTask(Point(p.x - 1, p.y - 1), p.dist);
    addTask(Point(p.x, p.y - 1), p.dist);
    addTask(Point(p.x + 1, p.y - 1), p.dist);
    addTask(Point(p.x - 1, p.y), p.dist);
    addTask(Point(p.x, p.y), p.dist);
    addTask(Point(p.x + 1, p.y), p.dist);
    addTask(Point(p.x - 1, p.y + 1), p.dist);
    addTask(Point(p.x, p.y + 1), p.dist);
    addTask(Point(p.x + 1, p.y + 1), p.dist);
  }
  if (found)
    printf("%d\n", dist - 1);
  else
    printf("-1\n");
}
