Dice = Struct.new(:labels) do
  def self.init(labels)
    labels = labels.dup.freeze
    raise ArgumentError, "length of labels should be 6" unless labels.count == 6

    new(labels)
  end

  def rotate(direction)
    case direction
    when ?N then rotate_northward
    when ?S then rotate_southward
    when ?E then rotate_eastward
    when ?W then rotate_westward
    else raise ArgumentError, "direction should be N, S, E or W, but #{direction} was given."
    end
  end

  def rotate_westward
    a,b,c,d,e,f = labels
    self.class.init([c,b,f,a,e,d])
  end

  def rotate_eastward
    c,b,f,a,e,d = labels
    self.class.init([a,b,c,d,e,f])
  end

  def rotate_southward
    a,b,c,d,e,f = labels
    self.class.init([e,a,c,d,f,b])
  end

  def rotate_northward
    e,a,c,d,f,b = labels
    self.class.init([a,b,c,d,e,f])
  end

  def [](index)
    raise ArgumentError, "index should be in 1 to 6" unless (1..6).include?(index)

    labels[index-1]
  end

  def top
    self[1]
  end
end

labels = gets.split.map(&:to_i)
dice = Dice.init(labels)

dice_last_state = gets.chomp.chars.reduce(dice) {|dice, direction|
  dice.rotate(direction)
}

p dice_last_state.top
