This page is still under construction.

Parts of this page are still being built. What you see may change.

Knight Moves: Black Edition

Time limit1sMemory limit1024 MB

Summary
Given an N by N board with N up to 1e15, find the fewest knight moves between two cells.
Level

Hard8 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

You have a chessboard of size N×NN \times N. The rows and columns are numbered from 1 to NN. A knight starts on the cell at row R1R_1 and column C1C_1, and wants to reach the cell at row R2R_2 and column C2C_2. Move the knight from the starting cell to the destination in the minimum number of moves.

A knight's jump moves it 2 cells along one axis and 1 cell along the other. If the knight is at (A,B)(A, B), it may move to (A−2,B−1)(A-2, B-1), (A−2,B+1)(A-2, B+1), (A+2,B−1)(A+2, B-1), (A+2,B+1)(A+2, B+1), (A−1,B−2)(A-1, B-2), (A+1,B−2)(A+1, B-2), (A−1,B+2)(A-1, B+2), or (A+1,B+2)(A+1, B+2). The knight cannot leave the board.

Given NN, R1R_1, C1C_1, R2R_2, and C2C_2, determine the minimum number of steps needed to move the knight from (R1,C1)(R_1, C_1) to (R2,C2)(R_2, C_2).

Input

The first line contains a positive integer TT, the number of test cases. Each case is a line with five integers NN (3≤N≤10153 \le N \le 10^{15}), R1R_1, C1C_1, R2R_2, and C2C_2 (1≤R1,C1,R2,C2≤N1 \le R_1, C_1, R_2, C_2 \le N).

Output

For each test case, print "Case #i:" where ii is the test case number starting from 1, followed by the minimum number of steps. A solution is guaranteed to exist, so the knight can always reach the destination from the starting cell. Leave a blank line after the output of each test case. Follow the format shown in the sample output.

Examples1

  1. Example 1

    Input
    2
    5 1 1 2 3
    5 1 1 2 2
    
    Expected output
    Case #1: 1
    
    Case #2: 4