#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int vis[310][310];
int n, fx, fy;
const int dx[8] = {2, 1, -1, -2, -2, -1, 1, 2};
const int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
void bfs(int x, int y) {
queue<int> q1, q2;
q1.push(x);
q2.push(y);
while (!q1.empty()) {
int nowx = q1.front();
q1.pop();
int nowy = q2.front();
q2.pop();
if (nowx == fx && nowy == fy) return ;
else {
for (int i = 0; i < 8; ++ i) {
int tx = nowx + dx[i];
int ty = nowy + dy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= n && !vis[tx][ty]) {
vis[tx][ty] = vis[nowx][nowy] + 1;
q1.push(tx);
q2.push(ty);
}
}
}
}
}
int main() {
int t;
scanf("%d", &t);
while (t --) {
memset(vis, 0, sizeof vis);
scanf("%d", &n);
n++;
int sx, sy;
scanf("%d%d", &sx, &sy);
scanf("%d%d", &fx, &fy);
sx ++, sy++;
fx ++, fy++;
bfs(sx, sy);
printf("%d\n", vis[fx][fy]);
}
return 0;
}