# Include <iostream>
# Include <queue>
Using namespace STD;
Struct point
{
Int X, Y;
};
Point bufa [8] =
{
{-2, 1}, {-1, 2}, {1, 2}, {2, 1 },
{2,-1}, {1,-2}, {-1,-2}, {-2,-1}
};
Int n, a [305] [305], B [305] [305];
Int rule (int x, int y) // determines whether the Board meets the conditions
{
If (x> = 0 & x <n & Y> = 0 & Y <n)
Return 1;
Return 0;
}
Int dbfs (INT Sx, int Sy, int Tx, int ty)
{
For (INT I = 0; I <n; I ++)
For (Int J = 0; j <n; j ++)
A [I] [J] = B [I] [J] =-1; // not accessed
A [SX] [sy] = B [TX] [ty] = 0;
Queue <point> L, R;
Point sp = {Sx, Sy };
Point TP = {Tx, Ty };
L. Push (SP );
R. Push (TP );
Point CP;
While (! (L. Empty () & R. Empty ()))
{
If (! L. Empty () // forward wide search
{
CP = L. Front ();
L. Pop ();
For (INT I = 0; I <8; I ++)
{
Int x = CP. x + bufa [I]. X;
Int y = CP. Y + bufa [I]. Y;
If (rule (x, y ))
{
If (A [x] [Y] =-1) // has not been accessed
{
A [x] [Y] = A [Cp. x] [Cp. Y] + 1;
Point Np = {x, y };
L. Push (NP );
}
If (B [x] [Y]! =-1) // indicates the intersection of the search process
Return a [x] [Y] + B [x] [Y];
}
}
}
If (! R. Empty () // extensive reverse search
{
CP = R. Front ();
R. Pop ();
For (INT I = 0; I <8; I ++)
{
Int x = CP. x + bufa [I]. X;
Int y = CP. Y + bufa [I]. Y;
If (rule (x, y ))
{
If (B [x] [Y] =-1)
{
B [x] [Y] = B [Cp. x] [Cp. Y] + 1;
Point Np = {x, y };
R. Push (NP );
}
If (A [x] [Y]! =-1) // indicates the intersection of the search process
Return a [x] [Y] + B [x] [Y];
}
}
}
}
}
Int main ()
{
Int T;
Cin> T;
While (t --)
{
Cin> N;
Int Sx, Sy, TX, Ty;
Cin> SX> Sy> TX> ty;
If (SX = TX & Sy = ty)
Cout <0 <Endl;
Else
Cout <dbfs (sx, Sy, TX, Ty) <Endl;
}
Return 0;
}