s :: [Int] -> [Int]
s x = (x !! 4) : (x !! 0) : (x !! 2) : (x !! 3) : (x !! 5) : (x !! 1) : []

e :: [Int] -> [Int]
e x = (x !! 3) : (x !! 1) : (x !! 0) : (x !! 5) : (x !! 4) : (x !! 2) : []

n :: [Int] -> [Int]
n x = (x !! 1) : (x !! 5) : (x !! 2) : (x !! 3) : (x !! 0) : (x !! 4) : []

w :: [Int] -> [Int]
w x = (x !! 2) : (x !! 1) : (x !! 5) : (x !! 0) : (x !! 4) : (x !! 3) : []

turn :: [Int] -> String -> Int
turn list [] = head list
turn list (x : xs)
  | x == 'S' = turn (s list) xs
  | x == 'E' = turn (e list) xs
  | x == 'N' = turn (n list) xs
  | x == 'W' = turn (w list) xs

main = getContents >>= print . (\[a,b] -> turn (map read $ words a) b) . lines