Problem: The Kingdom of Aqua Canora Mystica is a very affluent and peaceful country, but around the kingdom, there are many evil monsters that kill people. So the king gave an order to you to kill the master monster.

You came to the dungeon where the monster lived. The dungeon consists of a grid of square cells. You explored the dungeon moving up, down, left and right. You finally found, fought against, and killed the monster.

Now, you are going to get out of the dungeon and return home. However, you found strange carpets and big rocks are placed in the dungeon, which were not there until you killed the monster. They are caused by the final magic the monster cast just before its death! Every rock occupies one whole cell, and you cannot go through that cell. Also, each carpet covers exactly one cell. Each rock is labeled by an uppercase letter and each carpet by a lowercase. Some of the rocks and carpets may have the same label.

While going around in the dungeon, you observed the following behaviors. When you enter into the cell covered by a carpet, all the rocks labeled with the corresponding letter (e.g., the rocks with ‘A’ for the carpets with ‘a’) disappear. After the disappearance, you can enter the cells where the rocks disappeared, but if you enter the same carpet cell again or another carpet cell with the same label, the rocks revive and prevent your entrance again. After the revival, you have to move on the corresponding carpet cell again in order to have those rocks disappear again.

Can you exit from the dungeon? If you can, how quickly can you exit? Your task is to write a program that determines whether you can exit the dungeon, and computes the minimum required time.



Input

The input consists of some data sets.

Each data set begins with a line containing two integers, W and H (3 ≤ W, H ≤ 30). In each of the following H lines, there are W characters, describing the map of the dungeon as W × H grid of square cells. Each character is one of the following:

* ‘@’ denoting your current position,
* ‘<’ denoting the exit of the dungeon,
* A lowercase letter denoting a cell covered by a carpet with the label of that letter,
* An uppercase letter denoting a cell occupied by a rock with the label of that letter,
* ‘#’ denoting a wall, and
* ‘.’ denoting an empty cell.



Every dungeon is surrounded by wall cells (‘#’), and has exactly one ‘@’ cell and one ‘<’ cell. There can be up to eight distinct uppercase labels for the rocks and eight distinct lowercase labels for the carpets.

You can move to one of adjacent cells (up, down, left, or right) in one second. You cannot move to wall cells or the rock cells.

A line containing two zeros indicates the end of the input, and should not be processed.

Output

For each data set, output the minimum time required to move from the ‘@’ cell to the ‘<’ cell, in seconds, in one line. If you cannot exit, output -1.

Examples

Input

8 3
########
#<A.@.a#
########
8 3
########
#<AaAa@#
########
8 4
########
#<EeEe@#
#FG.e#.#
########
8 8
########
#mmm@ZZ#
#mAAAbZ#
#mABBBZ#
#mABCCd#
#aABCDD#
#ZZcCD<#
########
0 0


Output

7
-1
7
27


Input

8 3

<A.@.a#

8 3

<AaAa@#

8 4

<EeEe@#
FG.e#.#

8 8

mmm@ZZ#
mAAAbZ#
mABBBZ#
mABCCd#
aABCDD#
ZZcCD<#

0 0


Output

7
-1
7
27

Solution (in CPP):

Solution:
#include <iostream>
#include <queue>
#include <cstring>
#include <map>
using namespace std;

class P{
public:
	int x,y,cost,bit;

	P(int _x,int _y,int _cost,int _bit){
		x = _x;
		y = _y;
		cost = _cost;
		bit = _bit;
	}

	bool operator<(const P &p)const{
		return cost > p.cost;
	}
};

int w,h;
int s[32][32];
char t[32][32];

int dx[] = {-1,1,0,0};
int dy[] = {0,0,-1,1};

int solve(int sx,int sy){
	priority_queue<P> open;
	map< int, map<int, map<int, bool> > > closed;

	open.push(P(sx,sy,0,0));

	while(!open.empty()){
		P p = open.top(); open.pop();

		if(closed[p.x][p.y][p.bit]) continue;
		closed[p.x][p.y][p.bit] = true;

		//cout<<t[p.y][p.x]<<" : "<<"("<<p.x<<","<<p.y<<","<<p.cost<<")"<<endl;

		if(t[p.y][p.x] == '<'){
			return p.cost;
		}

		for(int i=0;i<4;i++){
			int nx = p.x + dx[i];
			int ny = p.y + dy[i];

			if(t[ny][nx]=='#') continue;

			//¬¶ ... âÌbit½]
			if(s[ny][nx] < 0){
				open.push(P(nx,ny,p.cost+1,(p.bit ^ (1<<(-s[ny][nx])))));
			}
			//å¶
			else if(s[ny][nx] > 0){
				if( (p.bit & (1<<s[ny][nx])) != 0 ){
					open.push(P(nx,ny,p.cost+1,p.bit));
				}
			}
			else{
				open.push(P(nx,ny,p.cost+1,p.bit));
			}
		}
	}

	return -1;
}

int main(void){
	while(cin>>w>>h && (w||h)){
		int sx,sy;
		int alpha[26],size=1;
		memset(alpha,-1,sizeof(alpha));
		memset(s,0,sizeof(s));

		for(int i=0;i<h;i++){
			cin>>t[i];
			for(int j=0;j<w;j++){
				if('A' <= t[i][j] && t[i][j] <= 'Z'){
					int idx = t[i][j] - 'A';
					if(alpha[idx] == -1){
						alpha[idx] = size++;
					}
					t[i][j] = '.';
					s[i][j] = alpha[idx];
				}
				else if('a' <= t[i][j] && t[i][j] <= 'z'){
					int idx = t[i][j] - 'a';
					if(alpha[idx] == -1){
						alpha[idx] = size++;
					}
					t[i][j] = '.';
					s[i][j] = -alpha[idx];
				}
				else if(t[i][j] == '@'){
					sx = j;
					sy = i;
				}
			}
		}

		cout<<solve(sx,sy)<<endl;
	}

	return 0;
}