### constant

COLORS = {
  'k' => 0, 'b' => 1, 'r' => 2, 'm' => 3,
  'g' => 4, 'c' => 5, 'y' => 6, 'w' => 7
}

# top=x, east=y, north=z
DICE = [2, 1, 4]

DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

INF = 1 << 30

### classes

class Dice
  attr_accessor :d

  def initialize(d0 = DICE) @d = d0.clone end
  def ==(dc) @d == dc.d end
  def <=>(dc) @d <=> dc.d end
  def to_s() '(' + @d.join(',') + ')' end
  def top() @d[0] end
  def east() @d[1] end
  def north() @d[2] end

  def rot_x(n = 1)
    dc = Dice.new(@d)
    n.times do
      t = dc.d[1]; dc.d[1] = 7 - dc.d[2]; dc.d[2] = t
    end
    dc
  end

  def rot_y(n = 1)
    dc = Dice.new(@d)
    n.times do
      t = dc.d[2]; dc.d[2] = 7 - dc.d[0]; dc.d[0] = t
    end
    dc
  end

  def rot_z(n = 1)
    dc = Dice.new(@d)
    n.times do
      t = dc.d[0]; dc.d[0] = 7 - dc.d[1]; dc.d[1] = t
    end
    dc
  end

  def to_north() rot_y(3) end
  def to_south() rot_y(1) end
  def to_east()  rot_z(1) end
  def to_west()  rot_z(3) end
end

### main

$dcs = []

for zr in (0...4)
  for yr in (0...4)
    for xr in (0...4)
      dc = Dice.new.rot_x(xr).rot_y(yr).rot_z(zr)
      $dcs << dc if ! $dcs.include?(dc)
    end
  end
end
$dcs.sort!
$ndcs = $dcs.length
#p $dcs.map{|dc| dc.to_s}; p $ndcs; exit

$rots = []
for dc in $dcs
  $rots << [
    $dcs.index(dc.to_east), $dcs.index(dc.to_north),
    $dcs.index(dc.to_west), $dcs.index(dc.to_south)
  ]
end
#p $rots

loop do
  w, h = gets.split.map(&:to_i)
  break if (w | h) == 0

  hw = h * w
  flds = []
  spos = nil
  
  for y in (0...h)
    line = gets.strip
    for x in (0...w)
      pos = y * w + x
      case line[x]
      when '#'
        spos = pos
        flds[pos] = 7
      else
        flds[pos] = COLORS[line[x]]
      end
    end
  end

  vis = gets.strip.split('').map{|c| COLORS[c]}
  #p flds
  #p spos
  #p vis

  sidx = $dcs.index(Dice.new(DICE))

  dists = hw.times.map{$ndcs.times.map{6.times.map{INF}}}
  dists[spos][sidx][0] = 0

  q = [[spos, sidx, 0]]
  min_dist = nil
  
  while ! q.empty?
    upos, uidx, unxt = q.shift
    ud = dists[upos][uidx][unxt]
    nvd = ud + 1
    ux = upos % w
    uy = upos / w
    
    for di in (0...4)
      dx, dy = DXYS[di]
      vx = ux + dx
      vy = uy + dy

      if vx >= 0 && vx < w && vy >= 0 && vy < h
        vpos = vy * w + vx
        vfld = flds[vpos]
        vidx = $rots[uidx][di]
        vtop = $dcs[vidx].top
        
        if vfld == 7
          if dists[vpos][vidx][unxt] > nvd
            dists[vpos][vidx][unxt] = nvd
            q << [vpos, vidx, unxt]
          end
        elsif vfld == vtop && vfld == vis[unxt]
          vnxt = unxt + 1
          if vnxt >= 6
            min_dist = nvd
            break
          end

          if dists[vpos][vidx][vnxt] > nvd
            dists[vpos][vidx][vnxt] = nvd
            q << [vpos, vidx, vnxt]
          end
        end
      end

      break if min_dist
    end

    break if min_dist
  end

  puts (min_dist || 'unreachable')
end