{-
http://www.acm-japan.org/past-icpc/domestic2006/contest/all_ja.html#section_B
2007.06.07
nisikawa
-}
import Control.Arrow
import Data.List (nub)
revF (x, y) = (reverse x, y)
revS (x, y) = (x, reverse y)
revB (x, y) = (reverse x, reverse y)
revT (x, y) = (y, x)
splits s = [(take x s, drop x s) | x <- [1..(length s) - 1]]
newtype Train a b = T {run :: a -> b}
exec s = length $ nub $ map (\x -> fst x ++ snd x) $ concatMap (exec') $ splits s
where exec' x = let ret = run ((T id >>> f) &&& (T revT >>> f)) x in g (fst ret) ++ g (snd ret)
f = T id &&& T revF &&& T revS &&& T revB
g (x1, (x2, (x3, x4))) = [x1, x2, x3, x4]
instance Arrow Train where
arr f = T f
(T f) >>> (T g) = T $ g . f
first (T f) = T $ first f