#include "bits/stdc++.h"
using namespace std;
const int N = 310;
typedef pair<int, int> PII;
PII q[N * N];
int dist[N][N], d, n;
int dx[] = {1, 2, 2, 1, -1, -2, -2, -1}, dy[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int main(){
cin >> d;
int x1, y1, x2, y2;
while(d --) {
cin >> n >> x1 >> y1 >> x2 >> y2;
memset(dist, 0, sizeof dist);
int tt = - 1, hh = 0;
q[++tt] = {x1, y1};
while(hh <= tt) {
auto t = q[hh++];
if(t.first == x2 && t.second == y2) {
cout << dist[x2][y2] << endl;
break;
}else {
for(int i = 0; i < 8; i++) {
int x = t.first + dx[i];
int y = t.second + dy[i];
if(x >= 0 && x < n && y >= 0 && y < n && dist[x][y] == 0) {
dist[x][y] = dist[t.first][t.second] + 1;
q[++tt] ={x, y};
}
}
}
}
}
return 0;
}