#include <bits/stdc++.h>
using namespace std;
int xx[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int yy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
int main() {
  int x, y, z, i, t;
  int st1, st, en, en1;
  map<pair<int, int>, int> m;
  queue<pair<int, int> > q;
  while (cin >> st >> en >> st1 >> en1) {
    cin >> x;
    for (i = 0; i < x; i++) {
      cin >> y >> z >> t;
      for (int j = z; j <= t; j++) {
        m[make_pair(y, j)] = -1;
      }
    }
    m[make_pair(st, en)] = 0;
    q.push(make_pair(st, en));
    while (!q.empty()) {
      int xxx = q.front().first;
      int yyy = q.front().second;
      q.pop();
      for (int j = 0; j < 8; j++) {
        int n = xxx + xx[j];
        int l = yyy + yy[j];
        if (m[make_pair(n, l)] < 0) {
          m[make_pair(n, l)] = m[make_pair(xxx, yyy)] + 1;
          q.push(make_pair(n, l));
        }
      }
    }
    cout << m[make_pair(st1, en1)] << endl;
  }
  return 0;
}
