program rubiko_kubas;
  { 83 u‘davinys }
  const max = 30; { maksimalus kubo dydis }
        m_eile = max * max; { maksimalus eil‚s ilgis }
  type kubas = array [1..max, 1..max, 1..max] of integer;
       kubelis = record
                   x, y, z : integer;
                 end;
       eile = array [1..max *  max] of kubelis;
  var n : integer; { kubo dydis }
      k : kubas;  { pats kubas }
      { globalieji kintamieji }

  function galima (kb : kubelis) : boolean;
    { nustato, ar galima eiti  kubel kb }
  begin
    galima := false;
    if (kb.x in [1..n]) and (kb.y in [1..n]) and (kb.z in [1..n])
       { jei neižeita iž kubo rib— }
       then galima := k[kb.x, kb.y, kb.z] = -1
  end;
  procedure itraukti_i_eile (var e : eile;
                             kb : kubelis;
                             var uodega : integer);
    { traukia  eil‚s gal… kubel x }
  begin
    with e[uodega] do
      begin
        x := kb.x;
        y := kb.y;
        z := kb.z
      end;
    if uodega = m_eile
       then uodega := 1
       else uodega := uodega + 1
  end;

  procedure pasalinti_is_eiles (var galva : integer);
    { pažalina iž eil‚s pirm…j element… }
  begin
    if galva = m_eile
       then galva := 1
       else galva := galva + 1
  end;

  function kelias (a, b : kubelis) : integer;
    { iežko trumpiausio atstumo nuo kubelio a iki b }
    type gretimi = array [1..6] of record
                                     x, y, z : integer
                                   end;
    const c : gretimi = ((x:0; y:0; z:1), (x:0; y:1; z:0), (x:1; y:0; z:0),
                         (x:0; y:0; z:-1), (x:0; y:-1; z:0), (x:-1; y:0; z:0));

    var e : eile;
        i, galva, uodega : integer;
        kb, nk : kubelis;
  begin
    galva := 1;
    uodega := 1;
    k[a.x, a.y, a.z] := 0;
    itraukti_i_eile (e, a, uodega);
    while galva <> uodega do
      begin
        { paimamas pirmasis kubelis iž eil‚s }
        kb.x := e[galva].x;
        kb.y := e[galva].y;
        kb.z := e[galva].z;
        for i := 1 to 6 do  { gali b–ti žeži gretimi kubeliai }
          begin
            nk.x := kb.x + c[i].x;
            nk.y := kb.y + c[i].y;
            nk.z := kb.z + c[i].z;
            if galima (nk)
               then begin
                      k[nk.x, nk.y, nk.z] := k[kb.x, kb.y, kb.z] + 1;
                      itraukti_i_eile (e, nk, uodega);
                    end;
          end;
        pasalinti_is_eiles (galva);
      end;
    kelias := k[b.x, b.y, b.z]
  end;

  var f : text;
      a, b : kubelis;
      i, x, y, z, kliutys, rez : integer;
begin
  assign (f, 'kubas.dat');
  reset (f);
  readln (f, n);
  readln (f, a.x, a.y, a.z);
  readln (f, b.x, b.y, b.z);
  { kubelius, kuriais galima jud‚ti, pa‘ymime '-1' }
  for x := 1 to n do
    for y := 1 to n do
      for z := 1 to n do
        k[x, y, z] := -1;
  { kli–tis su‘ymime '-2' }
  readln (f, kliutys );
  for i := 1 to kliutys do
    begin
      readln (f, x, y, z);
      k[x, y, z] := -2;
    end;
  close (f);

  rez := kelias (a, b);

  assign (f, 'kubas.rez');
  rewrite (f);
  writeln (f, rez);
  close (f);
end.
