# ITP1_11_A: Dice 1

class Dice
    def initialize(labels)
      pips  = [:top, :front, :right, :left, :back, :bottom]
      @pips = Hash[ *pips.zip(labels).flatten ]
    end

    def roll_to(direction)
      rotate case direction
      when 'N'
        {
          :top    => :back,
          :back   => :bottom,
          :bottom => :front,
          :front  => :top
        }
      when 'S'
        {
          :top    => :front,
          :front  => :bottom,
          :bottom => :back,
          :back   => :top
        }
      when 'E'
        {
          :top    => :right,
          :right  => :bottom,
          :bottom => :left,
          :left   => :top
        }
      when 'W'
        {
          :top    => :left,
          :left   => :bottom,
          :bottom => :right,
          :right  => :top
        }
      end
    end

    def pip_of(side)
      @pips[side]
    end

  private
    # This assumes that the +dests+ are ordered properly.
    def rotate(dests)
      top = @pips[:top]
      dests.keys.reverse_each do |src|
        @pips[ dests[src] ] = @pips[src]
      end
      @pips[ dests[:top] ] = top
      self
    end
end

labels = gets.chomp.split.map &:to_i
directions = gets.chomp.split('')

dice = Dice.new labels
directions.each do |direction|
  dice.roll_to direction
end

puts dice.pip_of(:top)