注意一下移动的方式就行
import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException {
Solution s = new Solution();
String[] ss = read.readLine().split(" ");
int T = Integer.valueOf(ss[0]);
for (int i = 0; i < T; i++) {
out.println(s.solve(read, out));
}
out.flush();
}
}
class Solution{
public Solution(){}
static int N = 310;
int[][] g = new int[N][N];
int[][] d = new int[N][N];
int[] dx = {2, 2, -2, -2, 1, 1, -1, -1};
int[] dy = {1, -1, 1, -1, 2, -2, 2, -2};
int n, sx, sy, ex, ey;
void bfs () {
for (int i = 0; i < N; i++) Arrays.fill(d[i], -1);
Queue<AbstractMap.SimpleEntry<Integer, Integer>> q = new ArrayDeque<>();
q.add(new AbstractMap.SimpleEntry<>(sx, sy));
d[sx][sy] = 0;
while (!q.isEmpty()) {
AbstractMap.SimpleEntry<Integer, Integer> p = q.remove();
int px = p.getKey(), py = p.getValue();
for (int i = 0; i < 8; i++) {
int x = px + dx[i], y = py + dy[i];
if (x >= 0 && x < n && y >= 0 && y < n && d[x][y] == -1) {
d[x][y] = d[px][py] + 1;
q.add(new AbstractMap.SimpleEntry<>(x, y));
}
}
}
}
public int solve (BufferedReader read, PrintWriter out) throws IOException {
String[] ss1 = read.readLine().split(" ");
String[] ss2 = read.readLine().split(" ");
String[] ss3 = read.readLine().split(" ");
n = Integer.valueOf(ss1[0]);
sx = Integer.valueOf(ss2[0]);
sy = Integer.valueOf(ss2[1]);
ex = Integer.valueOf(ss3[0]);
ey = Integer.valueOf(ss3[1]);
bfs();
return d[ex][ey];
}
}
class Test{
public Test(){};
public void test () {
}
}