#include<iostream>
#include<algorithm>
#include<cstring>
#include <queue>
#define x first
#define y second
using namespace std;
const int N=310;
typedef pair<int,int> PII;
int dist[N][N];
int T;
int n;
int bx,by,ex,ey;
int bfs()
{
queue<PII> Q;
Q.push({bx,by});
memset(dist,-1,sizeof dist);
dist[bx][by]=0;
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
while(!Q.empty())
{
PII t=Q.front();Q.pop();
for(int i=0;i<8;i++)
{
int a=dx[i]+t.x,b=dy[i]+t.y;
if(a>0&&a<=n&&b>0&&b<=n&&dist[a][b]==-1)
{
dist[a][b]=dist[t.x][t.y]+1;
if(a==ex&&b==ey) return dist[a][b];
Q.push({a,b});
}
}
}
return dist[ex][ey];
}
int main()
{
ios::sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n>>bx>>by>>ex>>ey;
bx++,by++,ex++,ey++;
cout<<bfs()<<endl;
}
return 0;
}