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 <algorithm>
#include <cmath>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <cassert>
#include <functional>

using namespace std;

#define LOG(...) printf(__VA_ARGS__)
//#define LOG(...)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define CLR(a) memset((a), 0 ,sizeof(a))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = { -1, 0, 1, 0 }; const int dy[] = { 0, 1, 0, -1 };
struct pos{
	int x;
	int y;
	int s;//0-25 a-z
	int step;
};
struct po{
	int x;
	int y;
	int s;//0-25 a-z
};

bool operator<(const po left, const po right){
	return left.x == right.x ? left.y == right.y ?left.s<right.s : left.y < right.y : left.x < right.x;
}
bool operator==(const po left, const po right){
	return left.x == right.x&&left.y == right.y&&left.s == right.s;
}

int main() {
	int w, h;
	while (cin >> w >> h, w){
		vector<vector<char>> cell(h, vector<char>(w));
		set<char> sc;
		int sx;
		int sy;
		int gx;
		int gy;
		REP(i, h){
			REP(j, w){
				cin >> cell[i][j];
				//if ('a' <= cell[i][j] && cell[i][j]<='z')
				if (cell[i][j] == '@'){
					sy = i;
					sx = j;
					cell[i][j] = '.';
				}
				if (cell[i][j] == '<'){
					gy = i;
					gx = j;
					cell[i][j] = '.';
				}
			}
		}
		set<po> G;
		queue<pos> Q;
		int ans = -1;
		G.insert({ sx, sy, 0 });
		Q.push({ sx, sy, 0, 0 });

		while (!Q.empty()){
			pos p = Q.front(); Q.pop();
			if (p.x == gx&&p.y == gy){
				ans = p.step;
				break;
			}

			REP(i, 4){
				if (((('A' <= cell[p.y + dy[i]][p.x + dx[i]] && cell[p.y + dy[i]][p.x + dx[i]]<='Z') && (1 << (cell[p.y + dy[i]][p.x + dx[i]] - 'A'))  & p.s)) || (cell[p.y + dy[i]][p.x + dx[i]] == '.') || ('a' <= cell[p.y + dy[i]][p.x + dx[i]] && cell[p.y + dy[i]][p.x + dx[i]] <= 'z')){
					po c;
					c.x = p.x + dx[i];
					c.y = p.y + dy[i];
					c.s = p.s;
					if ('a' <= cell[p.y + dy[i]][p.x + dx[i]] && cell[p.y + dy[i]][p.x + dx[i]] <= 'z')
						c.s ^= 1 << (cell[p.y + dy[i]][p.x + dx[i]] - 'a');
					if (G.find(c) == G.end()){
						G.insert(c);
						Q.push({ c.x, c.y, c.s, p.step + 1 });
					}
				}
			}
		}
		cout << ans << endl;
	}
}