import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import Text.Printf

main = do
	[ n, k ] <- map read . words <$> getLine
	as <- replicateM n getLine

	let
		solve s = all ( ( `elem` s ) . intToDigit ) [ 0 .. k ]

	print . length . filter solve $ as

module Main where

import Control.Monad
import Data.List

str2int = read :: String -> Int

uniq [] = []
uniq [x] = [x]
uniq (x:y:xs) = if x == y then uniq (y:xs) else x : uniq (y:xs)

how_many p = foldl (\n x -> if p x then n + 1 else n) 0

digits b n
    | n < b = [n]
    | otherwise = mod n b : digits b (div n b)

good k n = [0 .. k] == filter (<= k) (uniq . sort $ digits 10 n)

solve (n:k:_) = do
    lines <- replicateM n getLine
    let numbers = map str2int lines
        in  return $ how_many (good k) numbers

main = do
    line <- getLine
    ans <- solve $ map str2int $ words line
    print ans

import Data.List
import Control.Monad

digs :: Integral a => a -> [a]
digs 0 = []
digs x = x `mod` 10 : digs (x `div` 10)

sol :: Integer -> [Integer] -> Integer
sol k xx = toInteger $ length $ filter (==True) (map (\v -> all (\i -> elem i (digs v)) [0..k]) xx)

main = do
  i <- getLine
  let [n, k] = map (\x -> read x :: Int) $ words i
  inputs <- replicateM n getLine
  let nums = map (\s -> read s :: Integer) inputs
  print $ sol (toInteger k) nums

import Control.Applicative
main :: IO ()
main = do
    firstLine <- map read <$> words <$> getLine
    length <$> filter (isGood (firstLine !! 1)) <$> map (read :: String -> Int) <$> lines <$> getContents >>= print

isGood :: Int -> Int -> Bool
isGood k n
    | k == -1 = True
    | otherwise = k `elem` (map (read . (:[])) $ show n) && isGood (k-1) n

{-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-}
{-# OPTIONS_GHC -O2 #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Array.Unboxed
import Data.Array.IO.Safe
import Data.Char
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
import Data.Maybe
import Data.Set (Set)
import qualified Data.Set as Set
-- import Data.Tree
import Data.Tuple
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import qualified Data.ByteString.Char8 as B
-- import Debug.Trace

getInts = fmap (map read . words) getLine
-- getInts = unfoldr (B.readInt . B.dropWhile isSpace) <$> B.getLine

main = do
  [n, k] <- getInts

  ss <- replicateM n getLine

  let
    f s = (take (k+1) $ sort $ nub s) == ['0'..chr (48+k)]

  print $ length $ filter f ss

import Control.Monad (liftM, replicateM)
import Data.Char (ord, intToDigit)

solve :: Int -> [Int] -> Int
solve k as  =  length $ filter itsK as
  where
    itsK a  =  all (flip elem (show a)) ['0' .. intToDigit k]

main :: IO ()
main  =  do
    [n, k]  <-  reads
    as  <-  replicateM n readLn
    print $ solve k as

  where

    reads :: Num a => IO [a]
    reads  =  liftM (map read . words) getLine
      where
        read ('-':s)  =  (-1) * read s
        read s  =  read' 0 s
        read' a ""  =  a
        read' a (c:s)  =  read' a' s
          where
            a'  =  10 * a + fromIntegral (ord c - ord '0')

import Control.Applicative ((<$>), (<*>))
import Data.Char (digitToInt)

main :: IO ()
main = print =<< solve <$> (read . (!!1) . words <$> getLine) <*> (lines <$> getContents)

solve :: Int -> [String] -> Int
solve k xs = length [ x | x <- xs, all (`elem`map digitToInt x) [0..k] ]

import Control.Applicative ((<$>), (<*>))
import Data.Char (intToDigit)

main :: IO ()
main = print =<< solve <$> (read . (!!1) . words <$> getLine) <*> (lines <$> getContents)

solve :: Int -> [String] -> Int
solve k xs = length [ x | x <- xs, all (`elem`x) ['0'..intToDigit k] ]

module Main where

import Control.Monad
import Data.List

str2int = read :: String -> Int

uniq [] = []
uniq [x] = [x]
uniq (x:y:xs) = if x == y then uniq (y:xs) else x : uniq (y:xs)

how_many p = foldl (\n x -> if p x then n + 1 else n) 0

digits b n
    | n < b = [n]
    | otherwise = mod n b : digits b (div n b)

good k n = [0 .. k] == filter (<= k) (uniq . sort $ digits 10 n)

solve (n:k:_) = replicateM n getLine >>= \lines ->
    let numbers = map str2int lines
    in  return $ how_many (good k) numbers

main = getLine >>= \line ->
    solve (map str2int (words line)) >>= print

import Control.Applicative ((<$>), (<*>))
import Data.Char (digitToInt)

main :: IO ()
main = print =<< solve <$> (read . (!!1) . words <$> getLine) <*> (lines <$> getContents)

solve :: Int -> [String] -> Int
solve k xs = length [ x | x <- xs, all (<=k) $ map digitToInt x ]

module Main where

import Control.Monad
import Data.List

str2int = read :: String -> Int

uniq [] = []
uniq [x] = [x]
uniq (x:y:xs) = if x == y then uniq (y:xs) else x : uniq (y:xs)

how_many p = foldl (\n x -> if p x then n + 1 else n) 0

digits b n
    | n < b = [n]
    | otherwise = mod n b : digits b (div n b)

good k n = [0 .. k] == (uniq . sort $ digits 10 n)

solve (n:k:_) = do
    lines <- replicateM n getLine
    let numbers = map str2int lines
        in  return $ how_many (good k) numbers

main = do
    line <- getLine
    ans <- solve $ map str2int $ words line
    print ans

import Data.List
import Control.Monad

main = do
  i <- getLine
  let [sn, sk] = words i
  let n = read sn :: Int
  let k = head sk
  inputs <- replicateM n getLine
  print $ length $ filter (\num -> foldl1 (&&) (map (\c -> c <= k) num)) inputs

import Data.List
import Control.Monad

digs :: Integral a => a -> [a]
digs 0 = []
digs x = x `mod` 10 : digs (x `div` 10)

sol :: Integer -> [Integer] -> Integer
sol k xx = toInteger $ length $ filter (\x -> (sort (nub (digs x))) == [0..k]) xx

main = do
  i <- getLine
  let [n, k] = map (\x -> read x :: Int) $ words i
  inputs <- replicateM n getLine
  let nums = map (\s -> read s :: Integer) inputs
  print $ sol (toInteger k) nums


main :: IO()

main = do
	phrase <- getLine
	print (PlainString (minshift phrase))

newtype PlainString = PlainString String
instance Show PlainString where
  show (PlainString s) = s

swapSub :: [Char] -> [Char]
swapSub "" = "" -- correct
swapSub (l1:str)
	| l1 > 'a' = (pred l1) : (swapSub str)
	| otherwise = l1:str

minshift :: [Char] -> [Char]
minshift "" = "" -- correct
minshift ('a':[]) = "z" -- correct
minshift (l1:str)
	| l1 > 'a' = swapSub (l1:str)
	| otherwise = l1 : (minshift str)

import Data.Array

main = do
    let a = listArray ('a','z') $ 'z':['a'..'y']
        solve s = map (a!) b ++ c
            where (b,c) = span (/='a') s
    b <- getLine
    if head b /= 'a'
        then putStrLn $ solve b
        else do
            let (pre,suf) = span (=='a') b
            if suf == "" then putStrLn (init pre ++ "z") else putStrLn $ pre ++ (solve suf)

main = interact $ solve False . head . words
solve _ [] = []
solve True f@('a':t) = f
solve True (l:t) = (pred l) : (solve True t)
solve False ('a':[]) = "z"
solve False ('a':t) = 'a' : (solve False t)
solve False (l:t) = (pred l) : (solve True t)

import Data.Char

main = interact $ h.head.lines
f = map g
g x
  | x == 'a' = 'z'
  | otherwise = chr ((ord x) - 1)

h x
  | x == (take (length x) ['a', 'a'..]) = (take ((length x)-1) ['a', 'a' ..] ++ "z")
  |otherwise = (takeWhile (=='a') x) ++ (f (takeWhile (/='a') (dropWhile (=='a') x))) ++ (dropWhile (/='a') (dropWhile (=='a') x))

import Data.Char

f :: Bool -> String -> String
f _ [] = []
f False ['a'] = ['z']
f True  ['a'] = ['a']
f _ (x:[]) = [chr((ord x) - 1)]
f True ('a':xs) = ('a':xs)
f False ('a':xs) = ('a':(f False xs))
f _ (x:xs) = [chr((ord x) - 1)] ++ f True xs

main = do
	s <- getLine
	putStrLn (f False s)

import Data.Char

prev c = chr ((ord c) - 1)

solve True ""          = ""
solve False "a"        = "z"
solve True ('a':cs)    = 'a' : cs
solve True (c:cs)      = prev c : solve True cs
solve False ('a':cs)   = 'a' : solve False cs
solve False (c:cs)     = prev c : solve True cs

main = getLine >>= putStrLn . solve False

import Data.Char

ant :: Char -> Char
ant = chr . pred . ord

letter :: Char -> Bool
letter c = ord c >= ord 'a' && ord c <= ord 'z'

process :: Bool -> String -> String
process _ []             = []
process False "a"        = "z"
process False ('a':rest) = 'a'     : process False rest
process False (c:rest)   = (ant c) : process True rest
process True ('a':rest)  = 'a'     : rest
process True (c:rest)    = (ant c) : process True rest

main :: IO ()
main = interact (process False . filter letter)

main :: IO()

main = do
	phrase <- getLine
	print (minshift phrase)

swapSub :: [Char] -> [Char]
swapSub "" = "" -- correct
swapSub (l1:str)
	| l1 > 'a' = (pred l1) : (swapSub str)
	| otherwise = l1:str

minshift :: [Char] -> [Char]
minshift "" = "" -- correct
minshift ('a':[]) = "z" -- correct
minshift (l1:str)
	| l1 > 'a' = swapSub (l1:str)
	| otherwise = l1 : (minshift str)

import Data.Array

main = do
    let a = listArray ('a','z') $ 'z':['a'..'y']
        solve s = map (a!) b ++ c
            where (b,c) = span (/='a') s
    b <- getLine
    if head b /= 'a'
        then putStrLn $ solve b
        else do
            let (pre,suf) = span (=='a') b
            putStrLn $ pre ++ (solve suf)

import Data.Array

main = do
	let a = listArray ('a','z') $ 'z':['a'..'y']
	(b,c) <- fmap (span (/='a')) getLine
	putStrLn $ map (a!) b ++ c

main = interact $ solve False
solve _ [] = []
solve True f@('a':t) = f
solve True (l:t) = (pred l) : (solve True t)
solve False ('a':[]) = "z"
solve False ('a':t) = 'a' : (solve False t)
solve False (l:t) = (pred l) : (solve True t)

main = interact $ solve False
solve _ [] = []
solve True f@('a':t) = f
solve True (l:t) = (pred l) : (solve True t)
solve False ('a':t) = 'a' : (solve False t)
solve False (l:t) = (pred l) : (solve True t)

main = interact $ solve False
solve _ [] = []
solve False "a" = "z"
solve True f@('a':t) = f
solve True (l:t) = (pred l) : (solve True t)
solve False ('a':t) = 'a' : (solve False t)
solve False (l:t) = (pred l) : (solve True t)

import Data.Char

main = interact $ h.head.lines
f = map g
g x
  | x == 'a' = 'z'
  | otherwise = chr ((ord x) - 1)

h x = (takeWhile (=='a') x) ++ (f (takeWhile (/='a') (dropWhile (=='a') x))) ++ (dropWhile (/='a') (dropWhile (=='a') x))

import Data.Char

f :: Bool -> String -> String
f _ [] = []
f False ['a'] = ['z']
f True  ['a'] = ['a']
f _ (x:[]) = [chr((ord x) - 1)]
f b ('a':xs) = ['a'] ++ f b xs
f _ (x:xs) = [chr((ord x) - 1)] ++ f True xs

main = do
	s <- getLine
	putStrLn (f False s)

import Data.Char

ant :: Char -> Char
ant = chr . pred . ord

process :: Bool -> String -> String
process _ []             = []
process False "a"        = "z"
process False ('a':rest) = 'a'     : process False rest
process False (c:rest)   = (ant c) : process True rest
process True ('a':rest)  = 'a'     : rest
process True (c:rest)    = (ant c) : process True rest

main :: IO ()
main = interact (process False)

import Data.Char

ant :: Char -> Char
ant = chr . pred . ord

process :: Bool -> String -> String
process _ []             = []
process False ('a':rest) = 'a'     : process False rest
process False (c:rest)   = (ant c) : process True rest
process True ('a':rest)  = 'a'     : rest
process True (c:rest)    = (ant c) : process True rest

main :: IO ()
main = interact (process False)


module Main where

import Data.List
import Data.Maybe
import qualified Data.Map.Strict as Map

main = do
  tStr <- getLine
  let t = read tStr :: Int
  input <- getContents
  let sols = map (format . solve . parse) $ take t $ sep $ lines input
  mapM_ putStrLn sols

sep (_:x:y:xs) = (x,y):(sep xs)

parse (x,y) = zip (phi x) (phi y)
  where phi = (map read) . words

format b
  | b = "YES"
  | otherwise = "NO"

solve ps = solve' False (-1) ps

solve' :: Bool -> Int -> [(Int, Int)] -> Bool
solve' _ _ [] = True
solve' _ _ ((a,b):_)
  | a < b = False
solve' False c ((a,b):xs)
  | b == 0 = solve' False (max a c) xs
  | otherwise = a-b >= c && solve' True (a-b) xs
solve' True c ((a,b):xs) =
  (a-b == c || (b == 0 && a <= c)) && solve' True c xs

{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad.State
import qualified Data.ByteString.Char8 as C
import Data.Maybe

--
import Data.Bool

solve :: ([Integer], [Integer]) -> Bool
solve (as, bs) = isJust $foldM g (0, False) $ zipWith f as bs
 where
  f a 0 = (a, False)
  f a b = (a - b, True)

  g (x, False) (y, False) = Just (max x y, False)
  g (x, False) (y, True)
    | x <= y = Just (y, True)
    | otherwise = Nothing
  g (x, True) (y, True)
    | x == y = Just (x, True)
    | otherwise = Nothing
  g a b = g b a

input :: Scanner ([Integer], [Integer])
input = do
  n <- int
  as <- n >< integer
  bs <- n >< integer
  pure (as, bs)

output :: Bool -> C.ByteString
output = bool "NO" "YES"

main :: IO ()
main = C.interact $ C.unlines . map (output . solve) . runScanner (numberOf input)

-- Scanner

type Scanner = State [C.ByteString]

runScanner :: Scanner a -> C.ByteString -> a
runScanner = runScannerWith C.words

runScannerWith :: (C.ByteString -> [C.ByteString]) -> Scanner a -> C.ByteString -> a
runScannerWith sp sc = evalState sc . sp

bstr :: Scanner C.ByteString
bstr = get >>= \case s : ss -> put ss >> pure s

read' :: (C.ByteString -> Maybe (a, C.ByteString)) -> Scanner a
read' q = fst . fromJust . q <$> bstr

int :: Scanner Int
int = read' C.readInt

integer :: Scanner Integer
integer = read' C.readInteger

(><) :: Int -> Scanner a -> Scanner [a]
(><) = replicateM

numberOf :: Scanner a -> Scanner [a]
numberOf = (int >>=) . flip (><)

-- boilerplate {{{1
-- vim: foldmethod=marker
-- pragmas {{{2
{-# LANGUAGE BangPatterns        #-}
{-# LANGUAGE DeriveFunctor       #-}
{-# LANGUAGE DeriveFoldable      #-}
{-# LANGUAGE DeriveTraversable   #-}
{-# LANGUAGE FlexibleContexts    #-}
{-# LANGUAGE LambdaCase          #-}
{-# LANGUAGE MultiWayIf          #-}
{-# LANGUAGE RankNTypes          #-}
{-# LANGUAGE StandaloneDeriving  #-}
{-# LANGUAGE TupleSections       #-}
{-# LANGUAGE TypeApplications    #-}

module Main where -- {{{2

import Control.Applicative
import Control.Arrow ( (|||), (&&&) )
import Control.Monad
import Control.Monad.ST
import Data.Array.IArray
import Data.Array.IO
import Data.Array.ST
import Data.Array.Unboxed
import Data.Bifunctor
import Data.Bits
import Data.Bool
import Data.Char
import Data.Foldable hiding ( sum, product )
import Data.Function
import Data.Functor
import Data.IntMap ( IntMap )
import Data.IntSet ( IntSet )
import Data.List hiding ( sum, product )
import Data.Map ( Map )
import Data.Maybe
import Data.Monoid
import Data.Ord
import Data.Semigroup ( Min(..), Max(..) )
import Data.Sequence ( Seq )
import Data.Set ( Set )
import Data.Text ( Text )
import Data.Traversable
import Data.Tuple
import Debug.Trace
import Prelude hiding ( sum, product )
import System.IO
-- import System.Random
import Text.Printf
import qualified Data.ByteString.Char8 as C
import qualified Data.IntMap           as IM
import qualified Data.IntSet           as IS
import qualified Data.Map              as LM
import qualified Data.Map.Strict       as M
import qualified Data.Sequence         as Q
import qualified Data.Set              as S
import qualified Data.Text             as T
import qualified Data.Text.IO          as T

-- utilites {{{2
readInt s = let Just (i,_) = C.readInt s in i :: Int
getInt = readInt <$> C.getLine
getInts = map readInt . C.words <$> C.getLine
putShows :: Show a => [a] -> IO ()
putShows = putStrLn . unwords . map show
ynq = putStrLn . bool "NO" "YES"
gcount g@(x:_) = (x, length g)
modulus = 10^9 + 7 :: Int
mod' x = rem x modulus
tri n = quot (n * n + n) 2
sum t     = foldl' (+) 0 t
product t = foldl' (*) 1 t
mprint :: Show a => String -> Maybe a -> IO ()
mprint s = maybe (putStrLn s) print
ifM :: Monad m => m Bool -> m a -> m a -> m a
ifM b t e = b >>= bool e t
whenM :: Monad m => m Bool -> m () -> m ()
whenM b t = ifM b t (pure ())

-- }}}1

main = getInt >>= flip replicateM_ docase

docase = do
    [n] <- getInts
    aa  <- getInts
    bb  <- getInts
    let x = maximum $ zipWith (-) aa bb
    let f a 0 = a <= x
        f a b | a < b = False
        f a b = a - b == x
    let cc = zipWith f aa bb
    -- traceShowM (x,aa,bb,cc)
    ynq $ and cc


{-# LANGUAGE Strict #-}
module Main where
import qualified Data.Sequence as S
import System.IO

main :: IO ()
main = do
  hSetBuffering stdin LineBuffering
  interact $ unlines . solve . take2Nl . lines

take2Nl :: [String] -> [String]
take2Nl (n : xs) = take (2* (read n)) xs

solve :: [String] -> [String]
solve xs = let
             n = div (Prelude.length xs) 2
             go :: Int -> [String] -> [S.Seq Int] -> [S.Seq Int]
             go 0 _ sq = sq
             go nn (s:ss) sq = case even nn of
                            True -> go (nn-1) ss sq
                            False -> go (nn-1) ss ((f s):sq)
                              where f :: String -> S.Seq Int
                                    f st = S.fromList (map read $ words st)
             lsq = go (2 * n) xs []
             f :: S.Seq Int -> String
             f = func
           in
             reverse (f <$> lsq)

func :: S.Seq Int -> String
func sq = let (a,b,c,suc) = S.foldlWithIndex (\(i, j, k, b) ind a->
                        case b of
                          0 ->
                            case compare a (S.index sq i) of
                              GT -> (ind, 0, 0, b+1)
                              EQ -> (ind, 0, 0, b+1)
                              LT -> (ind, 0, 0, b)

                          1 ->
                            case compare a (S.index sq i) of
                              GT -> (i, ind, k, b+1)
                              EQ -> (i, ind, k, b+1)
                              LT -> (ind, j, k, b)

                          2 ->
                            case compare a (S.index sq j) of
                              GT -> (j, ind, k, b)
                              EQ -> (j, ind, k, b)
                              LT -> (i, j, ind, b+1)

                          3 -> (i, j, k, 3)
                                                        ) (0,0,0,0)  sq

          in case suc of
             3 -> "YES\n" ++ show (a+1) ++ " " ++ show (b+1) ++ " " ++ show (c+1)
             _ -> "NO\n"

{- This code was written by
Russell Emerine - linguist,
mathematician, coder,
musician, and metalhead. -}
import Control.Monad
import Data.List

main = getLine >>= flip replicateM_ test . read

test = do
  getLine
  ps <- map read . words <$> getLine :: IO [Integer]
  case solve $ zip [1 ..] ps of
    Just is -> putStrLn "YES" >> putStrLn (unwords $ map show is)
    Nothing -> putStrLn "NO"

solve ((i, a) : ps@((j, b) : (k, c) : _))
  | a < b && b > c = Just [i, j, k]
  | otherwise = solve ps
solve _ = Nothing

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
--import safe Data.List (sort)
import Data.List

main :: IO ()
main =  interact $  lines >>> drop 1  >>> map (words >>> map read) >>> process >>> unlines

process :: [[Int]] -> [String]
process [] = []
process ([_] : xs : rest) = solve xs 1 : process rest

solve :: [Int] -> Int -> String
solve (z:y:x:xs) i | (z < y && y > x) = (++) "YES\n" $ concat $ intersperse " " $ map show [i, i+1, i+2]
                   | otherwise = solve (y:x:xs) (i+1)
solve _ _ = "NO\n"

{-# LANGUAGE Strict #-}
module Main where
import qualified Data.Sequence as S
import System.IO

main :: IO ()
main = do
  hSetBuffering stdin LineBuffering
  interact $ unlines . solve . take2Nl . lines

take2Nl :: [String] -> [String]
take2Nl (n : xs) = take (2* (read n)) xs

solve :: [String] -> [String]
solve xs = let
             n = div (Prelude.length xs) 2
             go :: Int -> [String] -> [S.Seq Int] -> [S.Seq Int]
             go 0 _ sq = sq
             go nn (s:ss) sq = case even nn of
                            True -> go (nn-1) ss sq
                            False -> go (nn-1) ss ((f s):sq)
                              where f :: String -> S.Seq Int
                                    f st = S.fromList (map read $ words st)
             lsq = go (2 * n) xs []
             f :: S.Seq Int -> String
             f = func
           in
             reverse (f <$> lsq)

func :: S.Seq Int -> String
func sq = let (a,b,c,suc) = S.foldrWithIndex (\ind a (i, j, k, b) ->
                        case b of
                          0 ->
                            case compare a (S.index sq i) of
                              GT -> (ind, 0, 0, b+1)
                              EQ -> (ind, 0, 0, b+1)
                              LT -> (ind, 0, 0, b)

                          1 ->
                            case compare a (S.index sq i) of
                              GT -> (i, ind, k, b+1)
                              EQ -> (i, ind, k, b+1)
                              LT -> (ind, j, k, b)

                          2 ->
                            case compare a (S.index sq j) of
                              GT -> (j, ind, k, b)
                              EQ -> (j, ind, k, b)
                              LT -> (i, j, ind, b+1)

                          3 -> (i, j, k, 3)
                                                        ) (0,0,0,0)  sq

          in case suc of
             3 -> "YES\n" ++ show (c+1) ++ " " ++ show (b+1) ++ " " ++ show (a+1)
             _ -> "NO\n"

{-# LANGUAGE Safe #-}

import safe Control.Arrow ((>>>))
--import safe Data.List (sort)
import Data.List

main :: IO ()
main =  interact $  lines >>> drop 1  >>> map (words >>> map read) >>> process >>> unlines

process :: [[Int]] -> [String]
process [] = []
process ([_] : xs : rest) = solve xs : process rest

solve :: [Int] -> String
solve (x1:x2:x3:xs) | (x1 < x2 && x2 > x3) = "YES\n" ++ (concat $ intersperse " " $ map show [x1, x2, x3])
                    | otherwise = solve (x2:x3:xs)
solve _ = "NO\n"


module Main where

import Data.List

splitOn :: Eq a => a -> [a] -> [[a]]
splitOn s ss = reverse $ splitOn' [] s (reverse ss) where
  splitOn' xs y []     = [xs]
  splitOn' xs y (z:zs) =
    if y == z
      then xs : splitOn' [] y zs
      else splitOn' (z:xs) y zs

counter :: Eq a => [a] -> [(Integer, a)]
counter list = counter' 1 (head list) (tail list) where
  counter' :: Eq a => Integer -> a -> [a] -> [(Integer, a)]
  counter' n x []     = [(n, x)]
  counter' n x (y:ys) =
    if x /= y
      then (n,x) : counter' 1 y ys
      else counter' (n + 1) y ys

answer :: (Ord a, Eq a) => [a] -> Bool
answer list = null . (filter odd) . (map fst) $ (counter . sort) list

main :: IO ()
main = do
  n <- getLine
  elemsLine <- getLine
  let
    elemsStrs = splitOn ' ' elemsLine
    elems :: [Int]
    elems = map read elemsStrs
  putStrLn $ if answer elems then "Agasa" else "Conan"

import Data.Bool
import Data.List
main = putStr . bool "Agasa" "Conan" . or . map (odd . length) . group . sortBy (flip compare) . map (read :: String -> Int) . tail . words =<< getContents

import Data.List

main = getLine >> getLine >>= putStrLn . solve . words

solve xs
    | all (even . length) $ group $ sort xs = "Agasa"
    | otherwise = "Conan"

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Maybe
-- import Debug.Trace

-- debug = flip trace

getCount :: Int -> Int -> [Int] -> Bool
getCount p c (a:[])
    | p == a = (c + 1) `mod` 2 == 0 -- `debug` ("first pass p = " ++ show p ++ ", c = " ++ show a)
    | otherwise = False -- `debug` ("second pass p = " ++ show p ++ ", c = " ++ show a)
getCount p c (a : as)
    | (p /= a) && (c `mod` 2 /= 0) = False -- `debug` ("third pass p = " ++ show p ++ ", c = " ++ show a)
    | p /= a = getCount a 1 as -- `debug` ("fourth pass p = " ++ show p ++ ", c = " ++ show a)
    | otherwise = getCount p (c + 1) as -- `debug` ("fifth pass p = " ++ show p ++ ", c = " ++ show a)

solve :: [Int] -> String
solve as
    | countBool == True = "Agasa"
    | otherwise = "Conan"
    where countBool = getCount (-1) 0 as

main :: IO ()
main = do
    n <- fst . fromJust . B.readInt <$> B.getLine
    tas <- map (fst . fromJust . B.readInt) . B.words <$> B.getLine
    let as = sort tas
    putStrLn $ solve as

import Control.Applicative
import Data.List
-- import Debug.Trace

-- debug = flip trace

getCount :: Int -> Int -> [Int] -> Bool
getCount p c (a:[])
    | p == a = (c + 1) `mod` 2 == 0 -- `debug` ("first pass p = " ++ show p ++ ", c = " ++ show a)
    | otherwise = False -- `debug` ("second pass p = " ++ show p ++ ", c = " ++ show a)
getCount p c (a : as)
    | (p /= a) && (c `mod` 2 /= 0) = False -- `debug` ("third pass p = " ++ show p ++ ", c = " ++ show a)
    | p /= a = getCount a 1 as -- `debug` ("fourth pass p = " ++ show p ++ ", c = " ++ show a)
    | otherwise = getCount p (c + 1) as -- `debug` ("fifth pass p = " ++ show p ++ ", c = " ++ show a)

solve :: [Int] -> String
solve as
    | countBool == True = "Agasa"
    | otherwise = "Conan"
    where countBool = getCount (-1) (0) as

main :: IO ()
main = do
    n <- (read :: String -> Int) <$> getLine
    tas <- map (read :: String -> Int) . words <$> getLine
    let as = sort tas
    putStrLn $ solve as

import Data.Bool
import Data.List
main = putStr . bool "Agasa" "Conan" . (\(ys, zs) -> odd (length ys) || odd (length zs)) . (\xs -> partition (== maximum xs) xs)  . map (read :: String -> Int) . tail . words =<< getContents

import Data.Bool
main = putStr . bool "Agasa" "Conan" . odd . length . (\xs -> filter (== maximum xs) xs)  . map (read :: String -> Int) . tail . words =<< getContents

import Control.Applicative
import Data.List

getHighestCount :: [Int] -> Int -> Int
getHighestCount [] m = 0
getHighestCount (x:xs) m
    | m == x = 1 + rem
    | otherwise = rem
    where rem = getHighestCount xs m

solve :: Int -> [Int] -> String
solve n as
    | (n == hiCount) && (n `mod` 2 == 0) = "Agasa"
    | otherwise = "Conan"
    where hiCount = getHighestCount as $ maximum as

main :: IO ()
main = do
    n <- (read :: String -> Int) <$> getLine
    tas <- map (read :: String -> Int) . words <$> getLine
    let as = sort tas
    putStrLn $ solve n as


{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies, FlexibleInstances #-}
{-# OPTIONS_GHC -O2 #-}

import Data.List
import Data.Maybe
import Data.Char
import Data.Array.IArray
import Data.Array.Unboxed (UArray)
import Data.Int
import Data.Ratio
import Data.Bits
import Data.Function
import Data.Ord
--import Control.Monad.State
import Control.Monad
import Control.Applicative
import Data.ByteString.Char8 (ByteString)
import qualified Data.ByteString.Char8 as BS
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Map (Map)
import qualified Data.Map as Map
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Data.Sequence (Seq, (<|), (|>), (><), ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import qualified Data.Foldable as F
import Data.Graph

parseInput = do
    n <- readInt
    replicateM n (BS.unpack <$> readLine)
  where
    readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace
    readInteger = state $ fromJust . BS.readInteger . BS.dropWhile isSpace
    readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace
    readLine = state $ BS.span (not . isEoln) . BS.dropWhile isEoln
    isEoln ch = ch == '\r' || ch == '\n'

main = putStr . unlines =<< solve . evalState parseInput <$> BS.getContents

type Type = Maybe Int -- Nothing = ErrType
                      -- Just x  = VoidType (* times x)

type TypeMapping = Map String Type

modifyType :: Int -> Type -> Type
modifyType _ Nothing = Nothing
modifyType delta (Just x)
    | x + delta >= 0 = Just (x + delta)
    | otherwise      = Nothing

solve :: [String] -> [String]
solve program = evalState (solving program) $ Map.fromList [("void", Just 0), ("errtype", Nothing)]

parseType :: String -> (String, Int)
parseType str = (mid, length hi - length lo)
  where
    (lo, midHi) = span (=='&') str
    (mid, hi) = span isAlpha midHi

getType :: TypeMapping -> (String, Int) -> Type
getType mapping (typeName, typeDelta) = modifyType typeDelta $ Map.findWithDefault Nothing typeName mapping

showType :: Type -> String
showType Nothing = "errtype"
showType (Just x) = "void" ++ replicate x '*'

solving :: [String] -> State TypeMapping [String]
solving [] = return []
solving (op:ops)
    | opName == "typedef" = do
        mapping <- get
        put $ Map.insert y (getType mapping $ parseType x) mapping
        solving ops
    | otherwise           = do
        mapping <- get
        (showType (getType mapping $ parseType z):) <$> solving ops
  where
    (opName:leftPart) = words op

    [x, y] = leftPart

    [z] = leftPart

----------------------------------------------------------------------
----------------------------------------------------------------------
----------------------------------------------------------------------

class (Monad m) => MonadState s m | m -> s where
	get :: m s
	put :: s -> m ()

modify :: (MonadState s m) => (s -> s) -> m ()
modify f = do
	s <- get
	put (f s)

gets :: (MonadState s m) => (s -> a) -> m a
gets f = do
	s <- get
	return (f s)

newtype State s a = State { runState :: s -> (a, s) }

instance Functor (State s) where
	fmap f m = State $ \s -> let
		(a, s') = runState m s
		in (f a, s')

instance Applicative (State s) where
    pure = return
    (<*>) = ap

instance Monad (State s) where
	return a = State $ \s -> (a, s)
	m >>= k  = State $ \s -> let
		(a, s') = runState m s
		in runState (k a) s'

instance MonadState s (State s) where
	get   = State $ \s -> (s, s)
	put s = State $ \_ -> ((), s)

evalState :: State s a -> s -> a
evalState m s = fst (runState m s)

execState :: State s a -> s -> s
execState m s = snd (runState m s)

mapState :: ((a, s) -> (b, s)) -> State s a -> State s b
mapState f m = State $ f . runState m

withState :: (s -> s) -> State s a -> State s a
withState f m = State $ runState m . f

state = State

import Data.Map hiding (map)

main = interact $ unlines . solve . tail . map words . lines

solve es = reverse $ snd $ foldl parse (singleton "void" (Just 0), []) es

parse (types,result) (s:d:x) = case x of
  [type1] -> (insert type1 value1 types, result)
  _       -> (types, toString value1 : result)
  where
  (amps, bs)    = span (== '&') d
  (type0, asts) = span (/= '*') bs
  value0 = findWithDefault Nothing type0 types
  value1 = typedef value0 (length amps) (length asts)

typedef (Just n) a b
  | n+b-a >= 0 = Just (n+b-a)
typedef _ _ _ = Nothing

toString (Just n) = "void" ++ replicate n '*'
toString Nothing  = "errtype"

module Main where
import Data.List
import qualified Data.Map as Map
import Control.Monad

parseT[]=(0,"")
parseT(h:t)=case h of
    '*' -> (c+1,n)
    '&' -> (c-1,n)
    ch -> (c,ch:n)
 where
  (c,n)=parseT t

m!k = join$Map.lookup k m

newt c n | r>=0 = Just r
 | otherwise = Nothing
 where r = c+n

mkt state t= state!n >>= newt c
 where(c, n)=parseT t

type Type = Maybe Int
type State = Map.Map String Type
process :: State -> [String] -> (State, [Type])
process state ["typedef",t,name] = (Map.insert name (mkt state t) state, [])
process state ["typeof",t] = (state,[mkt state t])

pv n="void"++replicate n '*'
pp :: Type -> String
pp=maybe "errtype" pv

main=interact$unlines.map pp.concat.snd.mapAccumL process(Map.singleton "void"$Just 0).map words.tail.lines

module Main where

import Control.Monad
import Data.List
import qualified Data.Map as Map

main = do
    n <- read `liftM` getLine
    foldM_ (\table _ -> do
            cmd <- words `liftM` getLine
            let ref = cmd !! 1
                name = cmd !! 2
                refName = filter (\ch -> ch /= '*' && ch /= '&') ref
                incCount = length $ filter (=='*') ref
                decCount = length $ filter (=='&') ref
                refCount = maybe (-1) id $ Map.lookup refName table
                count = if refCount < 0 then (-1) else max (-1) (refCount+incCount-decCount)
            if head cmd == "typedef"
                then return $ Map.insert name count table
                else ( putStrLn $ if count < 0 then "errtype" else "void" ++ replicate count '*' ) >>
                    return table
        ) (Map.singleton "void" 0) [1..n]

module Main where

import Control.Monad
import Data.List
import qualified Data.Map as Map

main = do
    n <- read `liftM` getLine
    foldM_ (\table _ -> do
            cmd <- words `liftM` getLine
            let ref = cmd !! 1
                name = cmd !! 2
                refName = filter (\ch -> ch /= '*' && ch /= '&') ref
                incCount = length $ filter (=='*') ref
                decCount = length $ filter (=='&') ref
                refCount = maybe (-1) id $ Map.lookup refName table
                count = max (-1) (refCount+incCount-decCount)
            if head cmd == "typedef"
                then return $ Map.insert name count table
                else ( putStrLn $ if count < 0 then "errtype" else "void" ++ replicate count '*' ) >>
                    return table
        ) (Map.singleton "void" 0) [1..n]


import Data.List (nub, transpose)

solve :: String -> String
solve  =  show . (flip mod 1000000007) . product . map (fromIntegral . length . nub) . transpose . tail . lines

main :: IO ()
main  =  interact solve

import Data.List
import Control.Applicative

main = do
  _ <- getLine
  cs <- lines <$> getContents
  print $ solve cs

solve :: [String] -> Integer
solve cs = flip mod 1000000007 $ foldl1' (*) $ map (toInteger.length.nub) $ transpose cs

import Data.List

result::[String]->Integer
result = foldl1 f.map (fromIntegral.length.nub)
	where f a b = (a*b)`mod`1000000007

main=interact$show.result.transpose.tail.lines

import Data.List

main :: IO()
main = interact work

work :: String -> String
work = show . solve . tail . lines

solve :: [String] -> Integer
solve strings = (product . map (fromIntegral . length . group . sort) . transpose $ strings) `mod` 1000000007

import System.IO
import Data.List

readL :: Int -> IO [String]
readL n = sub n []
  where sub 0 l = return l
        sub n l = do ll <- getLine
                     sub (n-1) (l ++ [ll])

flatten :: [[a]] -> [a]
flatten [[]] = []
flatten ([]:ys) = flatten ys
flatten ((x:xs):ys) = x:(flatten (xs:ys))

rotate :: [String] -> [String]
rotate l = sub l []
  where sub l acc
          | flatten l == [] = acc
          | otherwise       = sub (map tail l) ((foldl (\ac x -> (head x):ac) "" l):acc)

modInt :: Integer
modInt = 1000000007

main :: IO ()
main = do
  f <- getLine
  cs <- readL . head $ map read (words f)
  let rcs = rotate cs
      ans = foldl (\ac x -> ac * ((genericLength $ nub x)::Integer) `mod` modInt) (1::Integer) rcs
  putStrLn $ show ans

import Data.List
import Control.Monad

find_best grades =
    [i | (i, x) <- zip [1..] grades, x == best]
    where best = maximum grades

solve :: Ord a => [[a]] -> Integer
solve grades =
    foldl mul 1 (map count grades)
    where count = toInteger . length . group . sort
          mul a b = (a * b) `mod` (10^9 + 7)

main = do
  (n:_) <- (getLine >>= return . map (read::String->Int) . words)
  xs <- forM [1..n] (\_ -> getLine >>= (return ))
  -- print $ transpose xs
  print $ solve (transpose xs)

import Data.List

main = interact $ show . solve . getInput . lines

getInput :: [String] -> [String]
getInput (s:ss) = map (take m) (take n ss) where
    [n, m] = map read (words s)

solve :: [String] -> Integer
solve ss = solve' (transpose ss) 1 where
    solve' []     n = n
    solve' (s:ss) n = solve' ss (n * cnt `mod` 1000000007) where
        cnt = (toInteger . length . group . sort) s

import List

solve :: [[Char]] -> Integer
solve = foldl (\a x -> mod (a * x) (10 ^ 9 + 7)) 1 . map (genericLength . group . sort) . transpose
main = interact $ show . solve . tail . lines

import Data.List

main:: IO()
main = interact work

work :: String -> String
work = show . flip mod 1000000007 . solve . tail . lines

solve :: [String] -> Integer
solve = product . map (fromIntegral . length . group . sort) . transpose

import Data.List (nub, transpose)

solve :: String -> String
solve  =  show . product . map (fromIntegral . length . nub) . transpose . tail . lines

main :: IO ()
main  =  interact solve

import List

solve :: [[Char]] -> Integer
solve = foldl (\a x -> mod (a * x) (10 ^ 9 + 1)) 1 . map (genericLength . group . sort) . transpose
main = interact $ show . solve . tail . lines

import List

solve :: [[Char]] -> Integer
solve = product . map (genericLength . group . sort) . transpose
main = interact $ show . solve . tail . lines

import Data.List

result = foldl1 f.map (length.nub)
	where f a b = (a*b)`mod`1000000007

main=interact$show.result.transpose.tail.lines

import Data.List

main :: IO()
main = interact work

work :: String -> String
work = show . product . map (length . group . sort) . transpose . tail . lines

import System.IO
import Data.List

readL :: Int -> IO [String]
readL n = sub n []
  where sub 0 l = return l
        sub n l = do ll <- getLine
                     sub (n-1) (l ++ [ll])

flatten :: [[a]] -> [a]
flatten [[]] = []
flatten ([]:ys) = flatten ys
flatten ((x:xs):ys) = x:(flatten (xs:ys))

rotate :: [String] -> [String]
rotate l = sub l []
  where sub l acc
          | flatten l == [] = acc
          | otherwise       = sub (map tail l) ((foldl (\ac x -> (head x):ac) "" l):acc)

modInt :: Int
modInt = 1000000007

main :: IO ()
main = do
  f <- getLine
  cs <- readL . head $ map read (words f)
  let rcs = rotate cs
      ans = foldl (\ac x -> ac * (length $ nub x) `mod` modInt) 1 rcs
  putStrLn $ show ans

import Data.List
import Control.Monad

find_best grades =
    [i | (i, x) <- zip [1..] grades, x == best]
    where best = maximum grades

solve :: Ord a => [[a]] -> Int
solve grades =
    foldl mul 1 (map count grades)
    where count = length . group . sort
          mul a b = (a * b) `mod` (10^9 + 7)

main = do
  (n:_) <- (getLine >>= return . map (read::String->Int) . words)
  xs <- forM [1..n] (\_ -> getLine >>= (return ))
  -- print $ transpose xs
  print $ solve (transpose xs)