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>
const double pi = 3.141592653589793238462643383279;
using namespace std;
const long double EPS = 1e-6, PI = acos((long double)-1);
int r[101010], a[101010], b[101010];
map<int, vector<pair<int, int>>> vp;
map<int, vector<pair<int, int>>> vpp;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(4);
  int x0, y0, x1, y1;
  cin >> y0 >> x0 >> y1 >> x1;
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 0; i < (int)n; ++i) {
    cin >> r[i] >> a[i] >> b[i];
    vp[r[i]].push_back({a[i], b[i]});
  }
  for (auto e : vp) {
    sort(e.second.begin(), e.second.end());
    int l = -1, r = -1;
    for (int i = 0; i < e.second.size(); i++) {
      if (l == -1) {
        l = e.second[i].first;
        r = e.second[i].second;
      }
      if (i + 1 < e.second.size()) {
        if (r < e.second[i + 1].first) {
          vpp[e.first].push_back({r, l});
          l = -1;
          r = -1;
          continue;
        } else {
          r = max(r, e.second[i + 1].second);
          continue;
        }
      } else {
        vpp[e.first].push_back({r, l});
        l = -1;
        r = -1;
        continue;
      }
    }
  }
  vector<long long> dist(n, (1LL << 60));
  map<pair<int, pair<int, int>>, int> idx;
  map<int, pair<int, pair<int, int>>> rev;
  int tmpp = 0;
  int spos = -1, gpos = -1;
  for (auto e : vpp) {
    for (auto f : e.second) {
      if (e.first == y0 && f.second <= x0 && x0 <= f.first) {
        spos = tmpp;
      }
      if (e.first == y1 && f.second <= x1 && x1 <= f.first) {
        gpos = tmpp;
      }
      idx[make_pair(e.first, f)] = tmpp;
      rev[tmpp++] = make_pair(e.first, f);
    }
  }
  if (spos == -1 || gpos == -1) {
    cout << -1 << endl;
    return 0;
  }
  dist[spos] = 0;
  queue<pair<int, pair<int, int>>> q;
  q.push(make_pair(spos, make_pair(x0, y0)));
  long long ans = (1LL << 60);
  while (q.size()) {
    auto p = q.front();
    q.pop();
    int pos = p.first;
    int x = p.second.first, y = p.second.second;
    int l1 = rev[pos].second.second, r1 = rev[pos].second.first;
    if (pos == gpos && l1 <= x1 && x1 <= r1) {
      ans = min(ans, dist[pos] + abs(x - x1));
      continue;
    }
    if (vpp.find(y + 1) != vpp.end()) {
      auto itr = lower_bound(vpp[y + 1].begin(), vpp[y + 1].end(),
                             make_pair(l1 - 1, -1));
      while (itr != vpp[y + 1].end()) {
        int l2 = (*itr).second, r2 = (*itr).first;
        if (r1 + 1 < l2 || r2 + 1 < l1) break;
        int cost;
        int ny = y + 1, nx = x;
        if (l2 <= x && x <= r2) {
          cost = 1;
          int iddd = idx[make_pair(y + 1, *itr)];
          if (dist[iddd] > dist[pos] + cost) {
            dist[iddd] = dist[pos] + cost;
            for (int t = -1; t <= 1; t++) {
              if (l2 <= t + nx && t + nx <= r2)
                q.push(make_pair(iddd, make_pair(t + nx, ny)));
            }
          }
        } else {
          if (r2 <= x) {
            cost = abs(x - r2);
            nx = r2;
          } else if (x <= l2) {
            cost = abs(x - l2);
            nx = l2;
          }
          int iddd = idx[make_pair(y + 1, *itr)];
          if (dist[iddd] > dist[pos] + cost) {
            dist[iddd] = dist[pos] + cost;
            q.push(make_pair(iddd, make_pair(nx, ny)));
          }
        }
        itr++;
      }
    }
    if (vpp.find(y - 1) != vpp.end()) {
      auto itr = lower_bound(vpp[y - 1].begin(), vpp[y - 1].end(),
                             make_pair(l1 - 1, -1));
      while (itr != vpp[y + 1].end()) {
        int l2 = (*itr).second, r2 = (*itr).first;
        if (r1 + 1 < l2 || r2 + 1 < l1) break;
        int cost;
        int ny = y + 1, nx = x;
        if (l2 <= x && x <= r2) {
          cost = 1;
          int iddd = idx[make_pair(y - 1, *itr)];
          if (dist[iddd] > dist[pos] + cost) {
            dist[iddd] = dist[pos] + cost;
            for (int t = -1; t <= 1; t++) {
              if (l2 <= t + nx && t + nx <= r2)
                q.push(make_pair(iddd, make_pair(t + nx, ny)));
            }
          }
        } else {
          if (r2 <= x) {
            cost = abs(x - r2);
            nx = r2;
          } else if (x <= l2) {
            cost = abs(x - l2);
            nx = l2;
          }
          int iddd = idx[make_pair(y - 1, *itr)];
          if (dist[iddd] > dist[pos] + cost) {
            dist[iddd] = dist[pos] + cost;
            q.push(make_pair(iddd, make_pair(nx, ny)));
          }
        }
        itr++;
      }
    }
  }
  if (ans == (1LL << 60)) {
    cout << -1 << endl;
  } else
    cout << ans << endl;
  return 0;
}
