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
#include <iostream>
#include <queue>
#include <map>
using namespace std;
typedef pair<int,int>P;//bit/コストxy


//ここからbit判定
int bit_data[26];
void bit_set(){
	bit_data[0]=1;
	for(int i=1;i<26;i++)bit_data[i]=bit_data[i-1]*2;
}
bool bit(int base,char check){
	if('A'<=check&&check<='Z'){
		int che=check-'A';
		int res=base&bit_data[che];
		if(!res)return 0;
		else return 1;
	}
	else if(check=='#'){
		return 0;
	}
	else{
		return 1;
	}
}
int bit_change(int base,char change){
	int che=change-'a';
	int res=base^bit_data[che];
	return res;
}
//ここまでbit判定

int xv[]={0,1,0,-1};
int yv[]={1,0,-1,0};

int main(){
	bit_set();
	int W,H;
	
	while(cin>>W>>H,H){
		queue<P>Q;
		map<P,int>flag;
		char land[32][32];
		for(int i=0;i<=W+1||i<=H+1;i++)land[i][0]=land[i][H+1]=land[0][i]=land[W+1][i]='#';
		for(int i=1;i<=H;i++){
			for(int j=1;j<=W;j++){
				cin>>land[j][i];
				if(land[j][i]=='@'){
					Q.push(make_pair(0,j*100+i));//どこのフラグも立っていない&0コストxy
					flag[make_pair(0,j*100+i)]=1;
				}
			}
		}
		int end=1;
		for(int b,cost,x,y;!Q.empty();Q.pop()){
			b=Q.front().first;
			cost=Q.front().second/10000;
			x=Q.front().second/100%100;
			y=Q.front().second%100;
			
			//cout<<"#"<<b<<" "<<cost<<" "<<x<<" "<<y<<endl;
			
			if(land[x][y]=='<'){
				cout<<cost<<endl;
				end=0;
				break;
			}
			if('a'<=land[x][y]&&land[x][y]<='z'){
				b=bit_change(b,land[x][y]);
			}
			for(int i=0;i<4;i++){
				x=Q.front().second/100%100+xv[i];
				y=Q.front().second%100+yv[i];
				if(bit(b,land[x][y])&&flag[make_pair(b,x*100+y)]==0){
					flag[make_pair(b,x*100+y)]=1;
					Q.push(make_pair(b,(cost+1)*10000+x*100+y));
				}
				
			}
		}
		if(end)cout<<-1<<endl;
	}
	return 0;
}
