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<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_W = 30;
const int MAX_H = 30;
const int MAX_HW = MAX_H * MAX_W;
const int SWBITS = 8;
const int MAX_SW = 1 << SWBITS;

const int INF = 1 << 30;

/* typedef */

typedef pair<int,int> pii;

enum { F_EMPTY, F_WALL };

/* global variables */

int w, h, hw, nsw;
int spos, gpos;
int flds[MAX_HW], swtbl[26];
int dists[MAX_HW][MAX_SW];
int dpos[4];

/* subroutines */

/* main */

int main() {
  for (;;) {
    int w, h;
    cin >> w >> h;
    if (w == 0) break;

    hw = w * h;
    dpos[0] = 1, dpos[1] = -w, dpos[2] = -1, dpos[3] = w;
    nsw = 0;
    
    memset(flds, 0, sizeof(flds));
    memset(swtbl, -1, sizeof(swtbl));
    
    for (int y = 0; y < h; y++) {
      string line;
      cin >> line;
      for (int x = 0; x < w; x++) {
	int pos = y * w + x;
	int ch = line[x];

	if (ch == '@') spos = pos;
	else if (ch == '<') gpos = pos;
	else if (ch == '#') flds[pos] = F_WALL;
	else if (ch >= 'a' && ch <= 'z') {
	  int id = ch - 'a';
	  if (swtbl[id] < 0) swtbl[id] = nsw++;
	  flds[pos] = 10 + swtbl[id];
	}
	else if (ch >= 'A' && ch <= 'Z')
	  flds[pos] = 20 + (ch - 'A');
      }
    }

    if (nsw > 8) {
      printf("Error: nsw=%d\n", nsw); exit(0);
    }
    
    for (int i = 0; i < MAX_HW; i++)
      for (int j = 0; j < MAX_SW; j++) dists[i][j] = INF;
    dists[spos][0] = 0;

    queue<pii> q;
    q.push(pii(spos, 0));

    int min_d = INF;
    
    while (! q.empty()) {
      pii u = q.front(); q.pop();

      int upos = u.first;
      int usw = u.second;
      int ud = dists[upos][usw];

      if (upos == gpos) {
	min_d = ud;
	break;
      }
      
      for (int di = 0; di < 4; di++) {
	int vpos = upos + dpos[di];
	int vsw = usw;
	int vfld = flds[vpos];

	if (vfld == F_WALL) continue;
	else if (vfld >= 10 && vfld <= 19) {	// sw
	  int sw = vfld - 10;
	  vsw ^= (1 << sw);
	}
	else if (vfld >= 20) {	// rock
	  int sw = swtbl[vfld - 20];
	  if (sw < 0 || ! (vsw & (1 << sw))) continue;
	}

	if (dists[vpos][vsw] >= INF) {
	  dists[vpos][vsw] = ud + 1;
	  q.push(pii(vpos, vsw));
	}
      }
    }

    if (min_d >= INF) cout << -1 << endl;
    else cout << min_d << endl;
  }

  return 0;
}