{"problem":{"name":"Wizard in Maze","description":{"content":"A maze is composed of a grid of $H \\times W$ squares - $H$ vertical, $W$ horizontal. The square at the $i$\\-th row from the top and the $j$\\-th column from the left - $(i,j)$ - is a wall if $S_{ij}$ i","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"abc176_d"},"statements":[{"statement_type":"Markdown","content":"A maze is composed of a grid of $H \\times W$ squares - $H$ vertical, $W$ horizontal.\nThe square at the $i$\\-th row from the top and the $j$\\-th column from the left - $(i,j)$ - is a wall if $S_{ij}$ is `#` and a road if $S_{ij}$ is `.`.\nThere is a magician in $(C_h,C_w)$. He can do the following two kinds of moves:\n\n*   Move A: Walk to a road square that is vertically or horizontally adjacent to the square he is currently in.\n*   Move B: Use magic to warp himself to a road square in the $5\\times 5$ area centered at the square he is currently in.\n\nIn either case, he cannot go out of the maze.\nAt least how many times does he need to use the magic to reach $(D_h, D_w)$?\n\n## Constraints\n\n*   $1 \\leq H,W \\leq 10^3$\n*   $1 \\leq C_h,D_h \\leq H$\n*   $1 \\leq C_w,D_w \\leq W$\n*   $S_{ij}$ is `#` or `.`.\n*   $S_{C_h C_w}$ and $S_{D_h D_w}$ are `.`.\n*   $(C_h,C_w) \\neq (D_h,D_w)$\n\n## Input\n\nInput is given from Standard Input in the following format:\n\n$H$ $W$\n$C_h$ $C_w$\n$D_h$ $D_w$\n$S_{11}\\ldots S_{1W}$\n$\\vdots$\n$S_{H1}\\ldots S_{HW}$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"abc176_d","tags":[],"sample_group":[["4 4\n1 1\n4 4\n..#.\n..#.\n.#..\n.#..","1\n\nFor example, by walking to $(2,2)$ and then using the magic to travel to $(4,4)$, just one use of magic is enough.\nNote that he cannot walk diagonally."],["4 4\n1 4\n4 1\n.##.\n####\n####\n.##.","\\-1\n\nHe cannot move from there."],["4 4\n2 2\n3 3\n....\n....\n....\n....","0\n\nNo use of magic is needed."],["4 5\n1 2\n2 5\n#.###\n####.\n#..##\n#..##","2"]],"created_at":"2026-03-03 11:01:14"}}