Professor-archaeologist Indiana Jones, armed only with a torch and a notebook, descends into the still-unexplored shaft of the Great Pyramid of Cheops. Recall that the pyramid is a rectangular grid of size n\times m cells. The starting position is cell in the northwest corner of the plan, and the goal of the expedition is to reach the sarcophagus chamber at cell .
An unexplored pyramid is an extremely dangerous place, full of traps and surprises, so help Indiana find a path to the sarcophagus chamber in as few steps as possible.
Along the way, he encounters:
massive monolithic blocks '#', which cannot be passed or destroyed;
empty corridors '.', through which he can move;
heavy stone slabs—doors 'A',,'F', decorated with the bas-reliefs of the gods;
golden scarab seals 'a',,'f', each of which activates the corresponding slab of the same type (for example, 'a',,'A', …, 'f',,'F').
After picking up a seal of a given type, all slabs of that type open immediately and remain open. Indiana can move only in the four orthogonal directions (up, down, left, right), one cell at a time.
You need to determine the minimum number of moves required to reach cell . If it is impossible, output -1.
The first line contains two integers and (1 \le n,m \le 200) — the dimensions of the pyramid map.
The next lines each contain characters describing the map:
'#' — wall;
'.' — empty cell;
'A',, 'F' — closed stone slab–doors;
'a',, 'f' — golden scarab seals.
It is guaranteed that the start cell and the goal cell are empty '.'.
Print a single integer — the minimum number of moves required to reach cell , or -1 if it is impossible.
Example 1
Example 2
In the first example, Indiana Jones first collects the golden scarab 'b' at position , after which he can open the door 'B' and reach the target cell in steps.
In the second example, it is impossible to reach the cell , so the output is -1.