mn :: [String] -> [String]
mn (a:b:c:d:e:f:_) = [b,f,c,d,a,e]
me :: [String] -> [String]
me (a:b:c:d:e:f:_) = [d,b,a,f,e,c]
ms :: [String] -> [String]
ms (a:b:c:d:e:f:_) = [e,a,c,d,f,b]
mw :: [String] -> [String]
mw (a:b:c:d:e:f:_) = [c,b,f,a,e,d]

move :: Char -> [String] -> [String]
move c xs
    | c == 'N'  = mn xs
    | c == 'E'  = me xs
    | c == 'S'  = ms xs
    | otherwise = mw xs

walk :: String -> [String] -> String
walk [] (y:ys) = y
walk (x:xs) ys = walk xs $ move x ys

solve :: [String] -> String
solve (a:b:c:d:e:f:xs:_) = walk xs ys
    where
        ys = [a,b,c,d,e,f]

main :: IO ()
main = interact $ (++"\n") . solve . words
