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 <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <string>
#include <queue>
#include <set>
#include <map>
#include <complex>
#include <iterator>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <cassert>

using namespace std;

#define EPS (1e-10)
#define EQ(a,b) (abs((a) - (b)) < EPS)
#define EQV(a,b) (EQ((a).real(),(b).real()) && EQ((a).imag(),(b).imag()))

typedef complex<double> P;
typedef long long ll;

int h,w;
char field[40][40];
map<char,int> idxs;
int idx;
bool passed[1<<12][31][31];
const int dy[]={-1,0,0,1};
const int dx[]={0,-1,1,0};

class Sit{
public:
  int s;
  int x;
  int y;
};

int bfs(pair<int,int> sp,pair<int,int> goal){
  queue<Sit> *prv=new queue<Sit>();
  queue<Sit> *nxt=new queue<Sit>();
  Sit ssit;
  ssit.s=0;
  ssit.x=sp.second;
  ssit.y=sp.first;
  prv->push(ssit);
  int dist=0;
  passed[0][sp.first][sp.second]=true;
  while(prv->size()){
    while(prv->size()){
      Sit cp=prv->front();prv->pop();
      if(cp.x==goal.second&&cp.y==goal.first){
	delete prv;delete nxt;
	return dist;
      }
      for(int i = 0; i < 4; i++){
	int ny=cp.y+dy[i];
	int nx=cp.x+dx[i];
	if(ny>=0&&nx>=0&&ny<h&&nx<w){
	  int bases=cp.s;
	  // carpet
	  if(field[ny][nx]>='a'&&field[ny][nx]<='z'){
	    int x=idxs[(char)(field[ny][nx])];
	    if((bases>>x)&1)
	      bases&=~(1<<x);
	    else
	      bases|=(1<<x);
	  }
	  if(field[ny][nx]!='#'
	     &&!passed[bases][ny][nx]){
	    // iwa
	    if(field[ny][nx]>='A'&&field[ny][nx]<='Z'){
	      if(idxs.find((char)(field[ny][nx]+32))
		 ==idxs.end()){
		continue;
	      }
	      int x=idxs[field[ny][nx]+32];
	      if(!((bases>>x)&1))
		continue;
	    }
	    passed[bases][ny][nx]=true;
	    Sit nsit;
	    nsit.s=bases;
	    nsit.x=nx;
	    nsit.y=ny;
	    nxt->push(nsit);
	  }
	}
      }
    }
    dist++;
    swap(prv,nxt);
  }
  delete prv;
  delete nxt;
  return -1;
}

int main(){
while(cin>>w>>h&&!(w==0&&h==0)){
    memset(passed,0,sizeof(passed));
    pair<int,int> sp,gp;
    idxs.clear();
    idx=0;
    for(int i = 0; i < h; i++){
      for(int j = 0; j < w; j++){
	cin>>field[i][j];
	if(field[i][j]=='@'){
	  sp.first=i;sp.second=j;
	  field[i][j]='.';
	}
	else if(field[i][j]=='<'){
	  gp.first=i;gp.second=j;
	  field[i][j]='.';
	}
	else if(field[i][j]!='#'&&field[i][j]!='.'){
	  char ch=field[i][j];
	  if(ch>='a'&&ch<='z'){
	    if(idxs.find(ch)==idxs.end())
	      idxs[ch]=idx++;
	  }
	}
      }
    }
    int res=bfs(sp,gp);
    cout<<res<<endl;
  }
  return 0;
}