The Great Chambers of the Pyramid of Cheops#11848

The Great Chambers of the Pyramid of Cheops

1 second
256 megabytes
Medium

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:

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.

Input

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 '.'.

Output

Print a single integer — the minimum number of moves required to reach cell , or -1 if it is impossible.

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

  • 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.