/* TU Darmstadt Programming Contest 2001
 * Problem: Knight's Moves
 * Author : Stefan Maier
 * Comment: Non-optimized version...
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void){
  int max_samples,size,from_x,from_y,to_x,to_y,sample,step,x,y;
  int *feld ;
  
  scanf("%d\n\n",&max_samples);

  for(sample=1;sample<=max_samples;sample++){
    scanf("%d\n",    &size);
    scanf("%d %d\n", &from_x, &from_y);
    scanf("%d %d\n\n", &to_x, &to_y);
 
    /* create field */
    feld=(int*) calloc(size*size,4);   
    
    /* set start position */
    step=1;
    feld[from_x+from_y*size]=1;

    while(feld[to_x+to_y*size]==0){
      /* searching for moves */
      for(x=0;x<size;x++)
        for(y=0;y<size;y++){
	  if(feld[x+y*size]==step){
	    /* set moves */
	    if(y-2 >= 0){
	      if(x-1 >= 0)   if(feld[x-1+(y-2)*size]==0) feld[x-1+(y-2)*size]=step+1;
	      if(x+1 < size) if(feld[x+1+(y-2)*size]==0) feld[x+1+(y-2)*size]=step+1;
	    }
	    if(y-1 >= 0){
	      if(x-2 >= 0)   if(feld[x-2+(y-1)*size]==0) feld[x-2+(y-1)*size]=step+1;
	      if(x+2 < size) if(feld[x+2+(y-1)*size]==0) feld[x+2+(y-1)*size]=step+1;	     
	    }
	    if(y+1 < size){
	      if(x-2 >= 0)   if(feld[x-2+(y+1)*size]==0) feld[x-2+(y+1)*size]=step+1;
	      if(x+2 < size) if(feld[x+2+(y+1)*size]==0) feld[x+2+(y+1)*size]=step+1;	     
	    }
	    if(y+2 < size){
	      if(x-1 >= 0)   if(feld[x-1+(y+2)*size]==0) feld[x-1+(y+2)*size]=step+1;
	      if(x+1 < size) if(feld[x+1+(y+2)*size]==0) feld[x+1+(y+2)*size]=step+1;
	    }
	  }
	}
      
      step++;
    }
    printf("%d\n",step-1);

    free(feld);

  }
  return 0;
}
