import Data.List

main = getLine >> getLine >>= print . solve

solve xs = numOps $ last $ filter check $ inits xs
    where numOps [] = length xs
          numOps s = length xs - (length s `div` 2) + 1

check s = uncurry (==) $ splitAt (length s `div` 2) s

module Main where

import Control.Monad
import Control.Applicative

main = do
 n <- (read::String -> Int) <$> getLine
 s <- getLine
 let max_length = maximum $ loop s n
 if max_length /= 0 then print $ n + 1 - max_length else print n

loop::String -> Int -> [Int]
loop x l = do
            end <- [0..l - 1]
            let single = take (end + 1) x
                double = take (end * 2 + 2) x
            if (single ++ single) == double
             then return (end + 1)
             else return 0

{-# LANGUAGE ViewPatterns #-}
module Main where

main = getLine >> getLine >>= (\xs -> let n = length xs in print $ n + (\x -> if x == 0 then 0 else 1 - x) (last (filter (ok xs) [0..n])))
ok xs n | (a, splitAt n -> (b, _)) <- splitAt n xs = a == b

solve :: String -> Int
solve str = min (length str) (1 + length str - copy)
    where copy = maximum $ filter (\x -> take x (drop x str) == take x str) [0..length str `div` 2]

main :: IO ()
main = getLine >> getLine >>= print . solve

module Main where

maxTrue :: Int -> [Bool] -> Int
maxTrue _ [] = 0
maxTrue init (False:xs) = maxTrue (init + 1) xs
maxTrue init (True:xs) = max init (maxTrue (init + 1) xs)

check :: String -> Int -> Bool
check xs maxRepeatLen = take maxRepeatLen xs == take maxRepeatLen (drop maxRepeatLen xs)

maxCopyLen :: String -> Int
maxCopyLen [] = 0
maxCopyLen [x] = 0
maxCopyLen xs = maxTrue 1 $ map (check xs) [1..length xs `div` 2]

eval :: String -> Int
eval str = min (length str) (length str + 1 - maxCopyLen str)

printRes :: String -> IO ()
printRes = print . eval

main :: IO ()
main = getLine >> getLine >>= printRes

module Main where

import Control.Monad
import Control.Applicative

main = do
 n <- (read::String -> Int) <$> getLine
 s <- getLine
 let max_length = maximum $ loop s n
 if max_length /= 0 then print $ n + 1 - max_length else print n

loop::String -> Int -> [Int]
loop x l = do
            start <- [0..l - 2]
            let partial_x = drop start x
            end <- [start + 1..l - 1]
            let single = take (end - start) partial_x
            let double = take ((end - start) * 2) partial_x
            if (single ++ single) == double
             then return (end - start)
             else return 0

module Main where

import Control.Monad
import Control.Applicative
import qualified Data.Map

main = do
 n <- (read::String -> Int) <$> getLine
 s <- getLine
 let max_length = maximum $ loop s n
 if max_length /= 0 then print $ n + 1 - max_length else print n

loop::String -> Int -> [Int]
loop x l = do
            start <- [0..l - 2]
            let partial_x = drop start x
            end <- [start + 1..l - 1]
            let single = take (end - start) partial_x
            let double = take ((end - start) * 2) partial_x
            if (single ++ single) == double
             then return (end - start)
             else return 0

module Main where

inner :: String -> String -> Int
inner [] _ = 0
inner _ [] = 0
inner (a:[]) (x:xs) = max 1 (inner (a:[x]) xs)
inner (a:as) (x:xs) = if a == x
  then max (inner ((a:as) ++ [x]) xs) (length $ takeWhile (\(c,b) -> c == b) (zip (a:as) (x:xs)))
  else inner ((a:as) ++ [x]) xs

ev :: String -> Int
ev (x:xs) = inner [x] xs

maxCopyLen :: String -> Int
maxCopyLen [] = 0
maxCopyLen [x] = 0
maxCopyLen (x:xs) = max (ev (x:xs)) (maxCopyLen xs)

eval :: String -> Int
eval str = min (length str) (length str + 1 - maxCopyLen str)

printRes :: String -> IO ()
printRes = print . eval

main :: IO ()
main = getLine >> getLine >>= printRes

module Main where

inner :: String -> String -> Int
inner _ [] = 0
inner "" (x:[]) = 0
inner (s:ss) (x:[]) = if s == x
  then 1
  else 0
inner "" (x:xs) = inner [x] xs
inner (a:as) (x:xs) = if a == x
  then max (1 + inner as xs) (inner ((a:as) ++ [x]) xs)
  else (inner ((a:as) ++ [x]) xs)

ev :: String -> Int
ev (x:xs) = inner [x] xs

maxCopyLen :: String -> Int
maxCopyLen [] = 0
maxCopyLen [x] = 0
maxCopyLen (x:xs) = max (ev (x:xs)) (maxCopyLen xs)

eval :: String -> Int
eval str = min (length str) (length str + 1 - maxCopyLen str)

printRes :: String -> IO ()
printRes = print . eval

main :: IO ()
main = getLine >> getLine >>= printRes


import List

un = map read . words

isParal [(a,b), (c,d)] = a == c || b == d

isNonnull [a, b] = a /= b

main = do
    s1 <- getLine
    s2 <- getLine
    s3 <- getLine
    s4 <- getLine
    let lines = map (\(a:b:c:d:_) -> sort [(a,b), (c,d)]) $ (map un [s1,s2,s3,s4] :: [[Integer]] )
        points = concat lines
        pointGroups = group $ sort points
        pointCounts = map length pointGroups --must all 2
        numOfPoints = length pointGroups --must be 4
        doubles = group $ sort lines --must be 4
        nonnulls = all (==True) $ map isNonnull lines
        parals = all (==True) $ map isParal lines
        is2d   = 1 < (length $ group $ map fst points) &&
                 1 < (length $ group $ map snd points)
    if (numOfPoints == 4 &&
        all (==2) pointCounts &&
        length doubles == 4 &&
        nonnulls &&
        is2d &&
        parals) then
        putStrLn "YES"
          else
           putStrLn "NO"

import List
import Control.Monad

solve p | sort p == sort q = "YES"
        | otherwise        = "NO"
    where q = nub [[(a,b),(c,d)] | a <- [x0, x1], c <-[a, x1], b <- [y0, y1], d <- [b, y1+0], (a /= c) /= (b /= d)]
          (x0, y0) = minimum $ concat p
          (x1, y1) = maximum $ concat p

main = replicateM 4 (fmap (map read . words) getLine) >>= (
       putStrLn . solve. map (sort.(\[a,b,c,d]->[(a,b),(c,d)])))

import List

un = map read . words

isParal [(a,b), (c,d)] = a == c || b == d

isNonnull [a, b] = a /= b

main = do
    s1 <- getLine
    s2 <- getLine
    s3 <- getLine
    s4 <- getLine
    let lines = map (\(a:b:c:d:_) -> sort [(a,b), (c,d)]) $ (map un [s1,s2,s3,s4] :: [[Int]] )
        points = concat lines
        pointGroups = group $ sort points
        pointCounts = map length pointGroups --must all 2
        numOfPoints = length pointGroups --must be 4
        doubles = group $ sort lines --must be 4
        nonnulls = all (==True) $ map isNonnull lines
        parals = all (==True) $ map isParal lines
    if (numOfPoints == 4 &&
        all (==2) pointCounts &&
        length doubles == 4 &&
        nonnulls &&
        parals) then
        putStrLn "YES"
          else
           putStrLn "NO"

import List

un = map read . words

isParal [(a,b), (c,d)] = a == c || b == d

isNonnull [a, b] = a /= b

main = do
    s1 <- getLine
    s2 <- getLine
    s3 <- getLine
    s4 <- getLine
    let l = map (\(a:b:c:d:_) -> [(a,b), (c,d)]) $ (map un [s1,s2,s3,s4] :: [[Int]] )
        ll = concat l
        points = group $ sort ll
        pointCounts = map length points --must all 2
        numOfPoints = length points --must be 4
        doubles = group $ sort l --must be 4
        nonnulls = all (==True) $ map isNonnull l
        parals = all (==True) $ map isParal l
    if (numOfPoints == 4 &&
        all (==2) pointCounts &&
        length doubles == 4 &&
        nonnulls &&
        parals) then
        putStrLn "YES"
          else
           putStrLn "NO"

import List

un = map read . words

isParal [(a,b), (c,d)] = a == c || b == d

isNonnull [a, b] = a /= b

main = do
    s1 <- getLine
    s2 <- getLine
    s3 <- getLine
    s4 <- getLine
    let l = map (\(a:b:c:d:_) -> [(a,b), (c,d)]) $ (map un [s1,s2,s3,s4] :: [[Int]] )
        ll = concat l
        points = group $ sort ll
        pointCounts = map length points --must all 2
        numOfPoints = length points --must be 4
        doubles = group $ sort l --must be 4
        nonnulls = all (==True) $ map isNonnull l
        parals = all (==True) $ map isParal l
    if (numOfPoints == 4 &&
        all (==2) pointCounts &&
        nonnulls &&
        parals) then
        putStrLn "YES"
          else
           putStrLn "NO"

import List

un = map read . words

isParal [(a,b), (c,d)] = a == c || b == d

isNonnull [a, b] = a /= b

main = do
    s1 <- getLine
    s2 <- getLine
    s3 <- getLine
    s4 <- getLine
    let lines = map (\(a:b:c:d:_) -> sort [(a,b), (c,d)]) $ (map un [s1,s2,s3,s4] :: [[Integer]] )
        points = concat lines
        pointGroups = group $ sort points
        pointCounts = map length pointGroups --must all 2
        numOfPoints = length pointGroups --must be 4
        doubles = group $ sort lines --must be 4
        nonnulls = all (==True) $ map isNonnull lines
        parals = all (==True) $ map isParal lines
    if (numOfPoints == 4 &&
        all (==2) pointCounts &&
        length doubles == 4 &&
        nonnulls &&
        parals) then
        putStrLn "YES"
          else
           putStrLn "NO"


import Data.Maybe

solve (n:m:i:j:a:b:[]) = catMaybes [try x y | x <- [1, n], y <- [1, m]]
  where
    try u v
        | i == u && j == v         = Just 0
        | i-a < 1 && i+a > n       = Nothing
        | j-b < 1 && j+b > m       = Nothing
        | i `mod` a /= u `mod` a   = Nothing
        | j `mod` b /= v `mod` b   = Nothing
        | ma `mod` 2 /= mb `mod` 2 = Nothing
        | otherwise                = Just $ max ma mb
      where
        ma = abs $ (u-i) `div` a
        mb = abs $ (v-j) `div` b

main = do
    input <- getLine
    case solve $ map read $ words input of
        [] -> putStr "Poor Inna and pony!\n"
        ans -> print $ minimum ans

import Data.Maybe (maybeToList)

solve n m i j a b = case concat $ map maybeToList [try x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls
    where
        try u v
            | i == u && j == v = Just 0
            | i-a < 1 && i+a > n = Nothing
            | j-b < 1 && j+b > m = Nothing
            | i `mod` a == u `mod` a &&
            j `mod` b == v `mod` b &&
            ma `mod` 2 == mb `mod` 2 = Just $ max ma mb
            | otherwise = Nothing
          where
            ma = abs $ (u-i) `div` a
            mb = abs $ (v-j) `div` b

getInts = fmap ((map read) . words) getLine

main = do
    [n, m, i, j, a, b] <- getInts
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import Data.Maybe

solve (n:m:i:j:a:b:[]) = catMaybes [try x y | x <- [1, n], y <- [1, m]]
  where
    try u v
        | i == u && j == v         = Just 0
        | i-a < 1 && i+a > n ||
          j-b < 1 && j+b > m ||
          i `mod` a /= u `mod` a ||
          j `mod` b /= v `mod` b ||
          ma `mod` 2 /= mb `mod` 2 = Nothing
        | otherwise                = Just $ max ma mb
      where
        ma = abs $ (u-i) `div` a
        mb = abs $ (v-j) `div` b

main = do
    input <- getLine
    case solve $ map read $ words input of
        [] -> putStr "Poor Inna and pony!\n"
        ans -> print $ minimum ans

import System.IO
import Data.Maybe (maybeToList)

check :: Integer -> Integer -> Bool
check l r = l `mod` 2 == r `mod` 2

try :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
try n m i j a b u v
    | i == u && j == v = Just 0
    | i-a < 1 && i+a > n = Nothing
    | j-b < 1 && j+b > m = Nothing
    | (i `mod` a == u `mod` a) &&
      (j `mod` b == v `mod` b) &&
      (check ma mb) = Just $ max ma mb
    | otherwise = Nothing
  where
    ma = abs $ (u-i) `div` a
    mb = abs $ (v-j) `div` b

solve :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
solve n m i j a b = case concat $ map maybeToList [try n m i j a b x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls

main :: IO()
main = do
    [n, m, i, j, a, b] <- fmap ((map read) .words) getLine
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import Data.Maybe (maybeToList)

try n m i j a b u v
    | i == u && j == v = Just 0
    | i-a < 1 && i+a > n = Nothing
    | j-b < 1 && j+b > m = Nothing
    | i `mod` a == u `mod` a &&
      j `mod` b == v `mod` b &&
      ma `mod` 2 == mb `mod` 2 = Just $ max ma mb
    | otherwise = Nothing
  where
    ma = abs $ (u-i) `div` a
    mb = abs $ (v-j) `div` b

solve n m i j a b = case concat $ map maybeToList [try n m i j a b x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls

getInts = fmap ((map read) . words) getLine

main = do
    [n, m, i, j, a, b] <- getInts
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import Data.Maybe

minimummaybe :: [Maybe Integer] -> Maybe Integer
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Integer] -> Maybe Integer
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || (mod ((div (abs (x0 - x1)) dx) + (div (abs (y0 - y1)) dy)) 2 == 1)   = Nothing
	| otherwise                                                    = Just (max ((div (abs (x0 - x1)) dx)) (div (abs (y0 - y1)) dy))

getans :: [Integer] -> String
getans (n : m : i : j : a : b : _)
	| (i == 1 || i == n) && (j == 1 || j == m)        = "0"
	| (i - a < 1 && i + a > n) || (j - b < 1 && j + b > m) ||  minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Integer])

import Data.Maybe (maybeToList)

check l r = l `mod` 2 == r `mod` 2

try n m i j a b u v
    | i == u && j == v = Just 0
    | i-a < 1 && i+a > n = Nothing
    | j-b < 1 && j+b > m = Nothing
    | i `mod` a == u `mod` a &&
      j `mod` b == v `mod` b &&
      ma `mod` 2 == mb `mod` 2 = Just $ max ma mb
    | otherwise = Nothing
  where
    ma = abs $ (u-i) `div` a
    mb = abs $ (v-j) `div` b

solve n m i j a b = case concat $ map maybeToList [try n m i j a b x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls

getInts = fmap ((map read) . words) getLine

main = do
    [n, m, i, j, a, b] <- getInts
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import System.IO
import Data.Maybe (maybeToList)

check :: Integer -> Integer -> Bool
check l r = l `mod` 2 == r `mod` 2

try :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
try i j a b u v
    | (i `mod` a == u `mod` a) &&
      (j `mod` b == v `mod` b) &&
      (check ma mb) = Just $ max ma mb
    | otherwise = Nothing
  where
    ma = abs $ (u-i) `div` a
    mb = abs $ (v-j) `div` b

solve :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
solve n m i j a b
    | i == a && j == b = Just 0
    | n == 1 || m == 1 = Nothing
    | i-a < 1 && i+a > n = Nothing
    | j-b < 1 && j+b > m = Nothing
    | otherwise = case concat $ map maybeToList [try i j a b x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls

main :: IO()
main = do
    [n, m, i, j, a, b] <- fmap ((map read) .words) getLine
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import System.IO
import Data.Maybe (maybeToList)

check :: Integer -> Integer -> Bool
check l r = l `mod` 2 == r `mod` 2

try :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
try i j a b u v
    | (i `mod` a == u `mod` a) &&
      (j `mod` b == v `mod` b) &&
      (check ma mb) = Just $ max ma mb
    | otherwise = Nothing
  where
    ma = abs $ (u-i) `div` a
    mb = abs $ (v-j) `div` b

solve :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Maybe Integer
solve n m i j a b = case concat $ map maybeToList [try i j a b x y | x <- [1, n], y <- [1, m]] of
                        [] -> Nothing
                        ls -> Just $ minimum ls

main :: IO()
main = do
    [n, m, i, j, a, b] <- fmap ((map read) .words) getLine
    case solve n m i j a b of
        Nothing -> putStr "Poor Inna and pony!\n"
        Just ans -> print ans

import Data.Maybe

minimummaybe :: [Maybe Integer] -> Maybe Integer
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Integer] -> Maybe Integer
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || (mod ((div (abs (x0 - x1)) dx) + (div (abs (y0 - y1)) dy)) 2 == 1)   = Nothing
	| otherwise                                                    = Just (max ((div (abs (x0 - x1)) dx)) (div (abs (y0 - y1)) dy))

getans :: [Integer] -> String
getans (n : m : i : j : a : b : _)
	| (i - a < 1 && i + a > n) || (j - b < 1 && j + b > m) ||  minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Integer])

import Data.Maybe

minimummaybe :: [Maybe Integer] -> Maybe Integer
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Integer] -> Maybe Integer
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || (mod ((div (abs (x0 - x1)) dx) + (div (abs (y0 - y1)) dy)) 2 == 1)   = Nothing
	| otherwise                                                    = Just (max ((div (abs (x0 - x1)) dx)) (div (abs (y0 - y1)) dy))

getans :: [Integer] -> String
getans (n : m : i : j : a : b : _)
	| (i /= n && i /= 1 && i - a < 1 && i + a > n) || (j /= m && j /= 1 && j - b < 1 && j + b > m) ||  minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Integer])

import Data.Maybe

minimummaybe :: [Maybe Integer] -> Maybe Integer
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Integer] -> Maybe Integer
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || (mod ((div (abs (x0 - x1)) dx) + (div (abs (y0 - y1)) dy)) 2 == 1)   = Nothing
	| otherwise                                                    = Just (max ((div (abs (x0 - x1)) dx)) (div (abs (y0 - y1)) dy))

getans :: [Integer] -> String
getans (n : m : i : j : a : b : _)
	| minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Integer])

import Data.Maybe

minimummaybe :: [Maybe Integer] -> Maybe Integer
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Integer -> Maybe Integer -> Maybe Integer
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Integer] -> Maybe Integer
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || ((div (abs (x0 - x1)) dx) /= (div (abs (y0 - y1)) dy))   = Nothing
	| otherwise                                                    = Just ((div (abs (x0 - x1)) dx))

getans :: [Integer] -> String
getans (n : m : i : j : a : b : _)
	| minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Integer])

import Data.Maybe

minimummaybe :: [Maybe Int] -> Maybe Int
minimummaybe [] = Nothing
minimummaybe (x : xs) = minmaybe x $ minimummaybe xs
	where
		minmaybe :: Maybe Int -> Maybe Int -> Maybe Int
		minmaybe Nothing Nothing = Nothing
		minmaybe (Just x) Nothing = Just x
		minmaybe Nothing (Just y) = Just y
		minmaybe (Just x) (Just y) = Just (min x y)

steps :: [Int] -> Maybe Int
steps (x0 : y0 : x1 : y1 : dx : dy : _)
	| ((mod (abs (x0 - x1)) dx) /= 0) || ((mod (abs (y0 - y1)) dy) /= 0) || ((div (abs (x0 - x1)) dx) /= (div (abs (y0 - y1)) dy))   = Nothing
	| otherwise                                                    = Just ((div (abs (x0 - x1)) dx))

getans :: [Int] -> String
getans (n : m : i : j : a : b : _)
	| minsteps == Nothing                                           = "Poor Inna and pony!"
	| otherwise                                                     = show $ fromJust $ minsteps
	where
		minsteps = minimummaybe [steps [i, j, 1, 1, a, b], steps [i, j, 1, m, a, b], steps [i, j, n, 1, a, b], steps [i, j, n, m, a, b]]

main = do
    input <- getLine
    putStr $ getans (map read (words input) :: [Int])


import Control.Applicative
import Control.Monad
import Data.List

main = do
		getLine
		n1<- sort <$> map read <$> words <$> getLine ::IO [Int]
		let n=  maximum $ map length $ group n1
		print n

import System.IO
import Data.List
import Data.Functor

main = do
    line <- getLine
    let n = read line :: Int

    line2 <- getLine
    let mywords = words line2

    let num = read <$> mywords :: [Int]

    let x = [length x | x <- group $ sort num]

    print $ maximum x

-- Codeforces 1003A
import Data.List

maxOccurences :: [Int] -> Int
maxOccurences list = maximum occurences
    where ordered = sort list
          grouped = group ordered
          occurences = map length grouped

main :: IO ()
main = do
    n <- getLine
    input <- getLine
    let numbers = map (read :: String -> Int) (words input)
    let maxOccur = maxOccurences numbers
    print maxOccur

import Data.List
import Data.Function

main = getLine >> getLine >>= print . length . maximumBy (compare `on` length) . group. sort . map (read :: String -> Int) . words

import Data.List

main=do
     e1<-getLine
     es<-getLine
     let xs=map read (words es)::[Int]
     print $ last $ sort $ map length $ group $ sort xs

import Control.Monad
import Data.Map as Map

main = readInput
   >>= putStrLn . show . maxMap . parse . words

readInput :: IO String
readInput = liftM2 go getLine getLine
  where go a b = b

parse :: [String] -> Map String Int
parse strs = fromListWith (+) $ Prelude.map (\s -> (s, 1)) strs

maxMap :: Map String Int -> Int
maxMap xs = Map.foldr max 0 xs

{-# LANGUAGE ScopedTypeVariables #-}

import Prelude
import Data.List
import Text.Printf
import Control.Monad
import Data.Ratio
import Data.Int
import Data.Functor

main :: IO ()
main = do
    (n :: Int) <- read <$> getLine
    (a :: [Int]) <- (map read . words) <$> getLine
    let answer = maximum . map length . group . sort $ a
    printf "%d" answer

{-# LANGUAGE TypeApplications #-}
import Data.List
main = interact $ show . maximum . map length . group . sort . map (read @Int) . tail . words

module Main where

import System.IO

coins :: Eq a => [a] -> Int
coins ns = length (coins' ns)

coins' :: Eq a => [a] -> [[a]]
coins' ns = foldl (flip coins'') [] ns

coins'' :: Eq a => a -> [[a]] -> [[a]]
coins'' a [] = [[a]]
coins'' a (xs:yss) | a `elem` xs = xs : (coins'' a yss)
                   | otherwise   = (a:xs):yss

main :: IO ()
main =
    do
       _ <- readInt
       str <- getLine
       ns <- return (map read $ words str :: [Int])
       putStrLn (show $ coins ns)

  where readInt :: IO Int
        readInt = readLn

--ghc 7.10

import Data.List(group,sort)

maxOccurrence :: (Ord a) => [a] -> Int
maxOccurrence = maximum . map length . group . sort

main = do
    nStr <- getLine
    aStr <- getLine
    let a = map read . words $ aStr :: [Int]
    print $ maxOccurrence a

import Data.List
main=interact$show.maximum.map length.group.sort.map (read::String->Int).tail.words

import Control.Monad
import Data.List

main = readInput
   >>= putStrLn . show . maxL . (map length) . group . words

readInput :: IO String
readInput = liftM2 go getLine getLine
  where go a b = b

maxL :: Ord a => [a] -> a
maxL (x:xs) = foldr go x xs
  where go a cand
          | a < cand = cand
          | otherwise = a


reduce :: String -> String
reduce [] = []
reduce [a] = [a]
reduce ('R':'U':xs) = 'D':reduce xs
reduce ('U':'R':xs) = 'D':reduce xs
reduce (x:xs) = x:reduce xs

reduceIter :: String -> String
reduceIter str = let str' = reduce str in if length str' == length str then str else reduce str'

solve :: String -> Int
solve = length . reduceIter

main :: IO ()
main = getLine >> getLine >>= print . solve

import Data.List

f ::String->Int
f []=0
f (x:[])=1
f ('U':'R':xs)=1+f xs
f ('R':'U':xs)=1+f xs
f (x:xs)=1+f xs

main = do
       e<-getLine
       es<-getLine
       print $ (f es)

module Main where

eval :: String -> Int
eval [] = 0
eval [x] = 1
eval ('U':'R':xs) = 1 + eval xs
eval ('R':'U':xs) = 1 + eval xs
eval (x:xs) = 1 + eval xs

printRes :: String -> IO ()
printRes str = print $ eval str

main :: IO ()
main = getLine >> getLine >>= printRes >>= return

module Main where

import Control.Monad
import Control.Applicative

main = do
 n <- (read::String -> Int) <$> getLine
 s <- getLine

 print . length $ foldl trans [] s

trans::String -> Char -> String
trans ('R':rest) 'U' = 'D':rest
trans ('U':rest) 'R' = 'D':rest
trans rest x = x:rest

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe

process [] = []
process [a] = [a]
process (a:b:c) | a=='R' && b=='U' = 'D' : (process c)
                | a=='U' && b=='R' = 'D' :(process c)
		| otherwise = a: (process (b:c))

main = do
		getLine
		a<-getLine
		print $ length $ process a

f [] c = c
f ('R':'U':as) c = f as (c+1)
f ('U':'R':as) c = f as (c+1)
f (a:as) c = f as (c+1)

main = do
   getLine
   as <- getLine
   print $ f as 0

main = getContents >>= print . solve . last . lines

solve [] = 0
solve ('R':'U':xs) = 1 + solve xs
solve ('U':'R':xs) = 1 + solve xs
solve (_:xs) = 1 + solve xs

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.Text as T

main = do
		getLine
		a<-getLine
		print $ T.length $ T.replace "RU" "D" $ T.replace "UR" "D" $ T.pack a

{-# LANGUAGE OverloadedStrings #-}

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import qualified Data.Text as T

main = do
		getLine
		a<-getLine
		let b=  T.length $ T.replace "RU" "D" $ T.replace "UR" "D" $ T.pack a
		let c=  T.length $ T.replace "UR" "D" $ T.replace "RU" "D" $ T.pack a
		print $ min b c


import Data.List
import Data.Char
import Data.Maybe
import Control.Monad
import Control.Applicative
import Data.Array
import Data.Bits

main = do
  [n,m,t] <- getList
  let list = [c_n_k n x * c_n_k m y | x <- [4..n], y <- [1..m], x+y == t]
  print $ sum list
  where
    getList = fmap (map read . words) getLine :: IO [Integer]
    c_n_k :: Integer -> Integer -> Integer
    c_n_k n k = product [1..n] `div` (product [1..k] * product [1..n-k])

{-
import HUnit

testSimple :: Test
testSimple  =  Test "TestSimple" $
    assertEq 1 (solve 4 1 5)

testInput :: Test
testInput  =  TestList "TestInput"
    [Test "1" $ assertEq 10 (solve 5 2 5),
     Test "2" $ assertEq 3 (solve 4 3 5)]

testReal :: Test
testReal  =  TestList "TestReal"
    [Test "1" $ assertEq 7 (solve 5 2 6)]

test :: IO ()
test  =  mapM_ run [testSimple, testInput, testReal]

--------------------------------------------------------------------------------
-}

reads :: Read a => IO [a]
reads = getLine >>= sequence . map readIO . words

solve :: Integer -> Integer -> Integer -> Integer
solve n m t  =  sum (map (\x -> (c n x) * (c m (t-x))) [x | x <- [4 .. t - 1]])
  where
    c n m  =  div (product [n-m+1 .. n]) (product [1..m])

main :: IO ()
main  =  do
    [n, m, t] <- Main.reads
    print $ solve n m t

--module CC where

combinator x 0 = 1
combinator x y = product [1..x] `div` (product [1..y] * product [1..x-y])

answer :: [Integer] -> Integer
answer [n,m,k] = func k m $ myFilter
                  $ zip [4..n] $ map (n `combinator`) [4..n]
  where
    myFilter xs = filter ((>=k).(+m).fst) xs

func :: Integer -> Integer -> [(Integer,Integer)] -> Integer
func k m xs = sum $ map f xs
  where
    f (x,y) = if k == x then 0 else y *( m `combinator` (k-x))

main = getLine >>= print.answer.map read.take 3.words

import Debug.Trace

main = interact $ show . count . map read . words
	where
		count (n:m:t:_) = foldr sumComb 0 [(x, t-x)| x<-[4..n], 0 < t-x, t-x <= m]
			where
				sumComb (x,y) k = (comb n x) * (comb m y) + k

comb n m = (perm n m) `div` (perm m m)
perm n 0 = 1
perm n m = n * (perm (n-1) (m-1))

main = interact $ show . count . map read . words
	where
		count (n:m:t:_) = sum [(comb n x) * (comb m $ t-x)| x<-[4..n], 0 < t-x, t-x <= m]
		comb n r = product [n-r+1..n] `div` product [1..r]

main = interact $ show . count . map read . words
	where
		count (n:m:t:_) = foldr sumComb 0 [x| x<-[4..n], 0 < t-x, t-x <= m]
			where
				sumComb x k = (comb n x) * (comb m $ t-x) + k
				comb n r = product [n-r+1..n] `div` product [1..r]

import Data.Int
import Data.Array
main = do
  [n,m,t] <- fmap (map read . words) getLine
  print $ sum [ ncr n n' * ncr m m' | n' <- [4..n], let m' = t - n', m' >= 1, m' <= m ]
ncr :: Int64 -> Int64 -> Int64
ncr = let a = array ((0,0),(30,30))
              [ ((n,m),if m == 0 || m == n
                       then 1
                       else ncr (n-1) (m-1) + ncr (n-1) m)
                | n <- [0..30], m <- [0..30] ]
      in \n m -> a!(n,m)

readInts :: String -> [Integer]
readInts str = map read (words str)

c :: Integer -> Integer -> Integer
c n n' = product [1..n] `div` product [1..n'] `div` product [1..n-n']

main :: IO ()
main = do
  [n, m, t] <- fmap readInts getLine
  let actors = [(n', m') | n'<-[4..n],let m'=t-n', m'>=1]
  print $ sum [ c n n' * c m m' | (n', m')<-actors]

{-# LANGUAGE TypeSynonymInstances #-}
{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE ForeignFunctionInterface, ScopedTypeVariables #-}
{-# LANGUAGE OverloadedStrings #-}
import Foreign
import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Alloc
import Debug.Trace
import Data.List
import Control.Monad
import Control.Monad.ST
import qualified Data.Array.Unboxed as UB
import Data.Array.ST
import Data.Array
import Data.Char
class IOtyp a where scan' :: String -> a; show' :: a -> String
instance IOtyp Int where scan' = read; show' = show
instance IOtyp Char where scan' (x:_) = x;show' = (:[])
instance IOtyp Float where scan' = read;show' = show
instance IOtyp Double where scan' = read;show' = show
instance IOtyp Integer where scan' = read;show' = show
instance IOtyp String where scan' = id;show' = id
instance (IOtyp a,IOtyp b) => IOtyp (a,b) where scan' = (\(x:y:_) -> (scan' x,scan' y)).words;show' (x,y) = show' x++' ':show' y
instance (IOtyp a,IOtyp b,IOtyp c) => IOtyp (a,b,c) where scan' = (\(x:y:z:_) -> (scan' x,scan' y,scan' z)).words;show' (x,y,z) = show' x++' ':show' y++' ':show' z
foreign import ccall "stdio.h puts" c_puts :: CString -> IO ()
scan :: (IOtyp a) => IO a; scan = getLine>>=return.scan'
scans :: (IOtyp a) => Int -> IO [a]; scans = (flip replicateM) scan
scanlist :: (IOtyp a) => IO [a]; scanlist = getLine>>=return.map scan'.words
scanlists :: (IOtyp a) => Int -> IO [[a]]; scanlists = (flip replicateM) scanlist
puts :: (IOtyp a) => a -> IO (); puts ans = do {str<-newCString $ show' ans;c_puts str;free str}

conb :: Int -> Int -> Integer
conb n m = if m>(n `div` 2)&&(n>m) then conbrec 1 1 (fromIntegral n) (fromIntegral (n-m)) else conbrec 1 1 (fromIntegral n) (fromIntegral m)
    where
      conbrec ans ans2 n m | m==0 = ans `div` ans2
                           | otherwise = conbrec (ans*n) (ans2*m) (n-1) (m-1)

solve :: Integer -> Int -> Int -> Int -> Int -> Integer
solve ans n m act actr | act<4 = ans
                       | otherwise = solve (ans + (conb n act) * (conb m actr)) n m (act-1) (actr + 1)

main = do (n, m, t) <- scan :: IO (Int, Int, Int)
          puts.show $ solve 0 n m (t-1) 1

{-# OPTIONS_GHC -O2 -optc-O2 #-}

-- solution for: http://codeforces.com/problemset/problem/131/C

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

choose n 0 = 1
choose 0 k = 0
choose n k = toInteger((choose (n-1) (k-1) * n) `div` k)

helper n m t x acc = do
       if (x == t) then acc
                   else helper n m t (x + 1) (acc + (choose n x) * (choose m (t-x)))

solve [n,m,t] = helper n m t 4 0

main =
  do all <- BS.getContents
     let (x, _)  = readMany readInt all
     print (solve x)
  where readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
          Just (x, r) -> let (xs, t) = readMany readf r
                         in  (toInteger(x) : xs, t)
          Nothing     -> ([], s)

fact :: Int -> Integer
fact n = product [1 .. toInteger n]
combo n m = div (fact n) $ (fact m) * (fact (n - m))
solve [n,m,t] = sum $ map ways [4 .. (t - 1)]
    where ways j = (combo m (t - j)) * (combo n j)
main = putStrLn.show.solve.map read.words =<< getLine

main :: IO()
main = interact work

work :: String -> String
work input = show $ solve n m t
    where [n, m, t] = map read . words $ input

factorial :: Integer -> Integer
factorial n = product [1..n]

binom :: Integer -> Integer -> Integer
binom n k
    | n >= k    = factorial n `div` factorial k `div` factorial (n - k)
    | otherwise = 0

solve :: Integer -> Integer -> Integer -> Integer
solve n m t = sum  [binom n i * binom m (t - i) | i <- [4..t - 1]]

comb n 0 = n
comb n m = div numer deno
    where numer = foldl1 (*) [(n-m+1) .. n]
          deno = foldl1 (*) [1..m]
s :: [Integer] -> Integer
s [n, m, t] = sum . map (\(x, y) -> (comb n x) * (comb m y)) $ [(x, t - x) | x <- [4..n], (t - x) <= m, x <= (t - 1)]

main = interact $ show . s . map read . words

import Data.Array
import Data.Function

main = interact solve

solve s =
    show $ c ! (n + m, t) - c ! (n, t) - (sum $ map (\x -> c ! (n, x) * c ! (m, t - x)) [0..3])
    where
        [n, m, t] = map read $ words s
        c = coefArr (n + m, t)

coefArr maxBounds = fix coefArr'
    where
        arrBounds = ((0, 0), maxBounds)
        coefArr' c =
            listArray arrBounds
            [ choose p |
              p <- range arrBounds,
              let choose (n, k) | k == 0    = 1
                                | n < k     = 0
                                | otherwise = c ! (n - 1, k) + c ! (n - 1, k - 1)
            ]

main = do
    [n, m, t] <- fmap (map read . words) getLine
    print $ c (n + m) t - c n t - (sum $ map (\x -> (c n x) * (c m (t - x))) [0..3])
    where
        c n k | n < k     = 0
              | otherwise = product [k + 1..n] `div` product [1..n - k]

import Data.Array (Array, Ix, array, (!), range)
import qualified Data.ByteString.Char8 as BS

memorize :: Ix a => (a -> b) -> (a, a) -> (a -> b)
memorize func rng = let memoArray = array rng [(i, func i) | i <- (range rng)]
                    in (!) memoArray

combination :: (Ix a, Num a, Num b) => (a, a) -> b
combination = memorize cmb ((0, 0), (100, 100))
    where cmb (a, b)
            | b == 0 || a == b = 1
            | otherwise        = combination (a - 1, b - 1) + combination (a - 1, b)

solve :: Int -> Int -> Int -> Integer
solve n m t = sum [ combination (n, i) * combination (m, t - i) |
                    i <- [4..t-1], i <= n, t - i >= 1, t - i <= m ]

main :: IO ()
main = do cs <- BS.getContents
          let ws = BS.words cs
              n  = bReadInt $ ws !! 0
              m  = bReadInt $ ws !! 1
              t  = bReadInt $ ws !! 2
          print $ solve n m t

bReadInt :: BS.ByteString -> Int
bReadInt = toNumber . BS.readInt

bReadInteger :: BS.ByteString -> Integer
bReadInteger = toNumber . BS.readInteger

toNumber :: Num a => Maybe (a, BS.ByteString) -> a
toNumber (Just (x, _)) = x
toNumber Nothing       = 0

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 07:57:44 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
--binom n k = product [k+1..n] `div` product [1..n-k]
binom n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        --print $ (binom n 4) * m * binom (n+m-5) (t-5)

        print $ sum [ (binom m g) * (binom n (t-g)) | g <- [1..min (t-4) m]]
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 07:55:14 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
--binom n k = product [k+1..n] `div` product [1..n-k]
binom n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        print $ (binom n 4) * m * binom (n+m-5) (t-5)

        print $ sum [ (binom m g) * (binom n (t-g)) | g <- [1..min (t-4) m]]
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 06:44:46 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
binom n k = product [k+1..n] `div` product [1..n-k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        print $ (binom n 4) * (binom m 1) * (binom (n+m-5) (t-5))
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)

import Data.List
import Data.Char
import Data.Maybe
import Control.Monad
import Control.Applicative
import Data.Array
import Data.Bits

main = do
  [n,m,t] <- getList
  let list = [toInteger $ c_n_k n x * c_n_k m y | x <- [4..n], y <- [1..m], x+y == t]
  print $ sum list
  where
    getList = fmap (map read . words) getLine :: IO [Int]
    c_n_k :: Int -> Int -> Int
    c_n_k n k = product [1..n] `div` (product [1..k] * product [1..n-k])

import Data.List
import Data.Char
import Data.Maybe
import Control.Monad
import Control.Applicative
import Data.Array
import Data.Bits

main = do
  [n,m,t] <- getList
  let list = [c_n_k n x * c_n_k m y | x <- [4..n], y <- [1..m], x+y == t]
  print $ sum list
  where
    getList = fmap (map read . words) getLine :: IO [Int]
    c_n_k n k = product [1..n] `div` (product [1..k] * product [1..n-k])

{-# OPTIONS_GHC -O2 -optc-O2 #-}

-- solution for: http://codeforces.com/problemset/problem/131/C

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

choose n 0 = 1
choose 0 k = 0
choose n k = toInteger((choose (n-1) (k-1) * n) `div` k)

solve [n,m,t] = (choose n 4) * (choose m 1) * (choose (n+m-5) (t-5))

main =
  do all <- BS.getContents
     let (x, _)  = readMany readInt all
     print (solve x)
  where readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
          Just (x, r) -> let (xs, t) = readMany readf r
                         in  (toInteger(x) : xs, t)
          Nothing     -> ([], s)

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 07:28:41 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
--binom n k = product [k+1..n] `div` product [1..n-k]
binom n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        print $ (binom n 4) * m * sum (map (\s -> binom (n+m-5) s) [0..(t-5)])
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 06:45:50 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
--binom n k = product [k+1..n] `div` product [1..n-k]
binom n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        print $ (binom n 4) * (binom m 1) * (binom (n+m-5) (t-5))
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)

-- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
-- File Name : 0001A.hs
-- Creation Date : 17-10-2012
-- Last Modified : Fri 19 Oct 2012 06:50:39 PM EEST
-- Created By : Greg Liras <gregliras@gmail.com>
--_._._._._._._._._._._._._._._._._._._._._.

{-# OPTIONS_GHC -O2 -optc-O2 #-}

import Data.Char (isSpace)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC

binom :: (Integral a) => a -> a -> a
--binom n k = product [k+1..n] `div` product [1..n-k]
binom n k = foldl (\z i -> (z * (n-i+1)) `div` i) 1 [1..k]

main :: IO ()
main =
    do
        all <- BS.getContents
        let Just (n, r1)    = readInteger all
        let Just (m, r2)    = readInteger r1
        let Just (t, _)    = readInteger r2
        print $ (binom n 4) * m * (binom (n+m-5) (t-5))
    where
        readInt s = BSC.readInt (BSC.dropWhile isSpace s)
        readInteger s = BSC.readInteger (BSC.dropWhile isSpace s)
        readMany readf s = case readf s of
            Just (x, r) ->  let
                                (xs, t) = readMany readf r
                            in
                                (x : xs, t)
            Nothing     ->  ([], s)


module Main where
import Control.Applicative
import Data.List
main :: IO ()
main = do
  _ <- getLine
  cs <- map read . words <$> getLine :: IO [Int]
  [x,y] <- map read . words <$> getLine :: IO [Int]
  let beginnerSizes = scanl1 (+) cs
      numKids = sum cs
      intermediateSizes = map (numKids -) beginnerSizes
  -- print beginnerSizes
  -- print intermediateSizes
  case find (\(bSize,iSize,_) -> x <= bSize && bSize <= y && x <= iSize && iSize <= y) $
            zip3 beginnerSizes intermediateSizes [1..] of
    Just (_,_,res) -> print (res + 1)
    Nothing -> print (0 :: Int)
  -- correctTimes <- map read . words <$> getLine :: IO [Int]
  -- wrongTimes <- map read . words <$> getLine :: IO [Int]
  -- let minCorrectTime = minimum correctTimes
  --     v = maximum (2*minCorrectTime : correctTimes)
  --     minWrongTime = minimum wrongTimes
  -- print $ if minWrongTime <= v then -1 else v

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

main = do
	getLine
	cs <- map read . words <$> getLine
	[ x, y ] <- map read . words <$> getLine

	let
		ok as = let tot = sum as in x <= tot && tot <= y

		ks = [ k + 1 | k <- [ 1 .. length cs ], ok ( take k cs ), ok ( drop k cs ) ]

	print $ if null ks then 0 else head ks

import Control.Applicative
main :: IO ()
main = do
    getLine
    cs <- map read <$> words <$> getLine
    ls <- map read <$> words <$> getLine
    let x = ls !! 0
        y = ls !! 1
    print . (solve x y) $ cs

solve :: Int -> Int -> [Int] -> Int
solve lb ub cs = helper 1 0 (sum cs) cs
    where
        helper k beginner intermediate left
            | okay beginner && okay intermediate = k
            | null left = 0
            | otherwise = helper (k+1) (beginner + head left) (intermediate - head left) (tail left)
        okay n = lb <= n && n <= ub

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

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Data.Array
-- import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
-- import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
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 System.IO
-- import Debug.Trace

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

main = do
  m <- readLn
  cs <- getInts
  [x, y] <- getInts

  let a = [k | k <- [1..m], let a = sum (take (k-1) cs), let b = sum (drop (k-1) cs), a `elem` [x..y], b `elem` [x..y]]

  print $ if null a then 0 else head a

import Control.Monad (liftM)
import Data.Char (ord)
import Data.List (intercalate)

solve :: (Int, Int) -> [Int] -> Int
solve (x, y) as  =  case answers of
    []  ->  0
    as  ->  head as
  where
    answers  =  map fst $ filter (good . snd) $ zip [1..] $ scanl (+) 0 as
    good s1  =  x <= s1 && s1 <= y && x <= s2 && s2 <= y
      where
        s2  =  sum as - s1

main :: IO ()
main  =  do
    getLine
    as  <-  reads
    [x, y]  <-  reads
    print $ solve (x, y) 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' (10 * a + fromIntegral (ord c - ord '0')) s

    prints :: Show a => [a] -> IO ()
    prints  =  putStrLn . intercalate " " . map show

solve :: [[Int]] -> Int
solve (_:c:[x, y]:_) =
  let cSum = sum c
      partialSums = zip [1..] $ scanl1 (+) c
      inRange :: Int -> Bool
      inRange q = q >= x && q <= y
      matching = filter (\(_, q) -> inRange q && inRange (cSum - q)) partialSums
  in
   if null matching then 0 else 1 + (fst $ head matching)

main :: IO ()
main =
  interact $ show . solve . map (map read . words) . lines

partsum :: [Int] -> [Int]
partsum [] = []
partsum ns = partsum' ns 0
  where partsum' [] _ = []
        partsum' (s:l) c = (c + s) : partsum' l (c + s)

main = do
  n <- read `fmap` getLine :: IO Int
  nums <- (map read . words) `fmap` getLine :: IO [Int]
  [x, y] <- (map read . words) `fmap` getLine :: IO [Int]
  let sum = partsum nums !! (n - 1)
  let fg = take n $ zip (0 : partsum nums) [1..n + 1]
  let res = head $ [snd q | q <- fg, fst q >= x, fst q <= y, sum - fst q >= x, sum - fst q <= y] ++ [0]
  putStrLn $ show res

#!/usr/bin/env runghc
main = do
  c <- getContents
  putStr . unlines . solve . lines $ c

solve::[String]->[String]
solve [] = []
solve (m:ci:xy:cs) = (show . f $ zip3 [2..] (scanl1 (+) c)  (tail . scanr (+) 0 $ c)): solve cs
  where
    c = map read . words $ ci
    [x,y] = map read . words $ xy
    f [] = 0
    f ((i,a,b):ss) = if x<=a && a<=y && x<=b && b<=y then i else f ss

main = do
    input <- getLine
    let m = head (getList input)
    input <- getLine
    let list = getList input
    input <- getLine
    let [x,y] = getList input
    putStr $ show $ getPoint 1 x y list 0 (sum list)

getList :: String -> [Int]
getList x = map read $ words x

inRange :: Int -> Int -> Int -> Bool
inRange v x y = (x <= v && v <= y)

getPoint :: Int -> Int -> Int -> [Int] -> Int -> Int -> Int
getPoint returnVar x y [] l r = 0
getPoint returnVar xLimit yLimit (x:xs) leftSum rightSum =
    if (inRange leftSum xLimit yLimit) && (inRange rightSum xLimit yLimit) then returnVar
    else getPoint (returnVar + 1) xLimit yLimit xs (leftSum + x) (rightSum - x)

solve :: Int -> [Integer] -> Integer -> Integer -> Integer
solve n c l r = find 1 sums
                where
                    parts = map ((flip splitAt) c) [0..n]
                    sums = map (\(x, y) -> (sum x, sum y)) parts
                    find _ [] = 0
                    find pos ((x, y):xs)
                        | all id [x >= l, x <= r, y >= l, y <= r] = pos
                        | otherwise = find (pos + 1) xs

main = do
    n <- getLine
    c <- fmap words getLine
    [x, y] <- fmap words getLine
    print $ solve (read n) (map read c) (read x) (read y)

import Control.Applicative
import Data.List

process _ _ _ _ _ [] = -1
process a b sums cs n (s:ss)  | and [ cs>=a,cs <=b, sums-cs >=a, sums-cs <=b] = n
                              | otherwise = process a b sums (cs+s) (n+1) ss

main= do
	getLine
	s<- map read. words <$> getLine ::IO [Int]
	[a,b]<- map read. words <$> getLine ::IO [Int]
	print $ (+) 1 $  process a b (sum s) 0 0 s

import Control.Applicative

fun :: Int -> Int -> Int -> Int -> Int -> [Int] -> Int
fun llevo total x y v [] = if (llevo>=x && llevo<=y && total-llevo>=x && total-llevo<=y) then v else 0
fun llevo total x y v (l:ls) = if (llevo>=x && llevo<=y && total-llevo>=x && total-llevo<=y) then v else fun (llevo+l) total x y (v+1) ls

main = do
       k <- getLine
       ls <- map read.words <$> getLine
       [x,y] <- map read.words <$> getLine
       let total = sum ls
       putStrLn.show $ fun 0 total x y 1 ls

import Data.List
find x
	| x>=0  = show x
	| otherwise = "No solution"

head' [] = (-1,([],[]))
head' x = head x

parse :: String -> [Int]
parse l = map read $ words l

cal _ students xy =
	let x = xy !! 0
	    y = xy !! 1
	in (+1) $ fst $ head' $filter (chk x y) $ sp students

sp ss =
	let si = [1..(length ss) -1 ]
	in map (\x -> (x , splitAt x ss)) si

chk x y tup = (chkk x y $ sum $ fst $ snd tup) && (chkk x y $  sum $ snd $ snd tup)
chkk x y n = (x <= n) && ( n <= y)

main = do
	l1 <- getLine
	l2 <- getLine
	l3 <- getLine
	putStr $ show $ cal (parse l1) (parse l2) (parse l3)

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

debug x = trace (show x) x
(|>) x f = f x; infixl 1 |>
(||>) mx f = mx >>= (return . f); infixl 1 ||>

solve cs total x y =
  solve' cs 0 2

  where
    solve' [] _ _ = 0
    solve' (c:cs) sum r =
      if check sum' then r
        else solve' cs sum' (r+1)
      where
        sum' = sum + c
    check sum =
      x <= sum  && sum <= y && x <= (total-sum) && (total-sum) <= y

main = do
  ls <- getContents ||> lines
  cs <- ls !! 1 |> words |> map read |> return
  [x,y] <- ls !! 2 |> words |> map read |> return
  print $ solve cs (sum cs) x y

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

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Control.Monad.State
import Data.Array
-- import Data.Array.IArray
import Data.Array.IO.Safe
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
-- import Data.Array.Unboxed
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Builder as BB
import Data.Char
import qualified Data.Foldable as F
import Data.Function
-- import Data.Graph
import Data.Int
import Data.List
import Data.List.Split
import Data.Maybe
import Data.Monoid
import Data.Ratio
import qualified Data.Sequence as Seq
import Data.Sequence (Seq)
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 System.IO
-- import Debug.Trace

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

main = do
  m <- readLn
  cs <- getInts
  [x, y] <- getInts

  let a = [s | s <- [1..m], let a = sum (take s cs), let b = sum (drop s cs), a `elem` [x..y], b `elem` [x..y]]

  print $ if null a then 0 else head a

import Control.Applicative
import Data.List

process _ _ _ _ _ [] = 0
process a b sums cs n (s:ss)  | and [ cs>=a,cs <=b, sums-cs >=a, sums-cs <=b] = n
                              | otherwise = process a b sums (cs+s) (n+1) ss

main= do
	getLine
	s<- map read. words <$> getLine ::IO [Int]
	[a,b]<- map read. words <$> getLine ::IO [Int]
	print $ process a b (sum s) 0 0 s

import Control.Applicative
import Data.List

process _ _ _ _ _ [] = 0
process a b sums cs n (s:ss)  | and [ cs>=a,cs <=b, sums-cs >=a, sums-cs <=b] = n
                              | otherwise = process a b sums (cs+s) (n+1) ss

main= do
	getLine
	s<- map read. words <$> getLine ::IO [Int]
	[a,b]<- map read. words <$> getLine ::IO [Int]
	print $ (+) 1 $  process a b (sum s) 0 0 s


import qualified Data.ByteString.Char8 as B
import Data.List

main = mapM_ (putStrLn . concatMap show) . solve . slice . map (toInteger . maybe 0 fst . B.readInt) . B.words =<< B.getContents

slice :: [Integer] -> [[Integer]]
slice (a : b : c : xs) = [a, b, c] : slice xs
slice xs = []

solve :: [[Integer]] -> [[Integer]]
solve ns = [[(sum [ns!!x'!!y' | [x',y'] <- [[x-1,y], [x,y-1], [x,y], [x,y+1], [x+1,y]], x'>=0, x'<3, y'>=0, y'<3] + 1) `mod` 2 | y <-[0..2]] | x <- [0..2]]

import Data.List
import Data.List.Split

(|>) x f = f x
main = interact solve

solve contents = let
  toggles = contents |> lines |> map words |> map (map read) :: [[Int]]
  states = toggleThem toggles
  in states |> chunksOf 3 |> unlines

toggleThem toggles = [state toggles x y | x <- [0..2], y <- [0..2]]

state toggles x y = let
  count = toggleCount toggles x y
  in if even count then '1' else '0'

toggleCount toggles x y = toggleTimes toggles (x - 1) y
  + toggleTimes toggles x y
  + toggleTimes toggles (x + 1) y
  + toggleTimes toggles x (y - 1)
  + toggleTimes toggles x (y + 1)

toggleTimes toggles x y = if isValid x y then toggles !! x !! y else 0

isValid x y = x >= 0 && x < 3 && y >= 0 && y < 3

import Data.List
import Control.Monad
import qualified Data.Text as T
import Data.Maybe

(f3, x, o, os )  = ((\a b c -> a . b . c), not, id, replicate 9 o)

to01 = map T.unpack . T.chunksOf 3 . T.pack .  map (\x -> if x then '1' else '0')

solve [a,b,c,d,e,f,g,h,i] =
    zipWith ($)
        (zipWith3 f3
            (zipWith3 f3 (if a==1 then [x,x,o,x,o,o,o,o,o] else os)
                         (if b==1 then [x,x,x,o,x,o,o,o,o] else os)
                         (if c==1 then [o,x,x,o,o,x,o,o,o] else os))
            (zipWith3 f3 (if d==1 then [x,o,o,x,x,o,x,o,o] else os)
                         (if e==1 then [o,x,o,x,x,x,o,x,o] else os)
                         (if f==1 then [o,o,x,o,x,x,o,o,x] else os))
            (zipWith3 f3 (if g==1 then [o,o,o,x,o,o,x,x,o] else os)
                         (if h==1 then [o,o,o,o,x,o,x,x,x] else os)
                         (if i==1 then [o,o,o,o,o,x,o,x,x] else os)))
        $ replicate 9 True

main = do
    a <- concat <$> replicateM 3 (map(read :: String -> Int) . words <$>  getLine)
    mapM_ putStrLn $ to01 $ solve $ map (`mod` 2) a

import Control.Monad (liftM)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getContents

solve :: [Int] -> [Int]
solve [a11, a12, a13,
       a21, a22, a23,
       a31, a32, a33]
  =  map (\n -> (n - 1) `mod` 2)
      [a11 + a12 + a21, a11 + a12 + a13 + a22, a12 + a13 + a23,
       a11 + a21 + a22 + a31, a12 + a21 + a22 + a23 + a32, a13 + a22 + a23 + a33,
       a21 + a31 + a32, a22 + a31 + a32 + a33, a23 + a32 + a33]

print' :: Show a => [a] -> IO ()
print' [a1, a2, a3, a4, a5, a6, a7, a8, a9]  =  do
    mapM_ (\x -> putStrLn (concatMap show x))
        [[a1, a2, a3], [a4, a5, a6], [a7, a8, a9]]

main :: IO ()
main  =  reads >>= print' . solve

main :: IO ()
main = getContents >>= mapM_ (putStrLn . concatMap show) . solve . map (map read . words) . lines

solve :: [[Int]] -> [[Int]]
solve xs = [ [ 1 - sum [ xs !! i' !! j' | j' <- [0..2], i' <- [0..2], abs (i - i') + abs (j - j') < 2 ] `mod` 2 | j <- [0..2] ] | i <- [0..2] ]
-- solve xs = foldl' f (replicate 3 $ replicate 3 1)
--                   [ (i, j, x) | (j, xs') <- zip [0..2] xs, (i, x) <- zip [0..2] xs' ]
--   where f :: [[Int]] -> (Int, Int, Int) -> [[Int]]
--         f ys (i, j, x) = [ [ if abs (i' - i) + abs (j' - j) < 2 && odd x then 1 - y else y
--                               | (i', y) <- zip [0..] ys' ] | (j', ys') <- zip [0..] ys ]

import Data.List (foldl')

main :: IO ()
main = getContents >>= mapM_ (putStrLn . concatMap show) . solve . map (map read . words) . lines

solve :: [[Int]] -> [[Int]]
solve xs = foldl' f (replicate 3 $ replicate 3 1)
                  [ (i, j, x) | (j, xs') <- zip [0..2] xs, (i, x) <- zip [0..2] xs' ]
  where f :: [[Int]] -> (Int, Int, Int) -> [[Int]]
        f ys (i, j, x) = [ [ if abs (i' - i) + abs (j' - j) < 2 && odd x then 1 - y else y
                              | (i', y) <- zip [0..] ys' ] | (j', ys') <- zip [0..] ys ]

main :: IO ()
main = getContents >>= mapM_ (putStrLn . concatMap show) . solve . map (map read . words) . lines

solve :: [[Int]] -> [[Int]]
solve xs = [ [ (1 + sum [ xs !! i' !! j' | j' <- [0..2], i' <- [0..2], abs (i - i') + abs (j - j') < 2 ]) `mod` 2 | j <- [0..2] ] | i <- [0..2] ]

import Control.Applicative
import Data.List
import Data.Array

q::[[Int]]
q=[[1,2,4],[1,2,3,5],[2,3,6],[1,4,7,5],[2,4,5,6,8],[3,5,6,9],[7,8,4],[8,7,9,5],[9,8,6]]

process::[Int]->Array Int Int->Int->[Int]
process [] a _ = elems a
process (x:xs) a n | even x = process xs a (n+1)
                   | otherwise = process xs (a//(map (\z->(z,1-a!z)) (q!!n))) (n+1)

chunks n [] = []
chunks n x = (take n x ): (chunks n (drop n x))

main= do
	x<- map read. words <$> getContents::IO [Int]
	let b= listArray (1,9) [1,1,1,1,1,1,1,1,1]
	putStrLn $ unlines $ map concat $  chunks 3 $ map show $ process x  b 0

import Data.List
import Data.List.Split

(|>) x f = f x
main = interact solve

solve contents = let
  toggles = contents |> lines |> map words |> map (map read) :: [[Int]]
  states = toggleThem toggles
  in states |> chunksOf 3 |> unlines |> show

toggleThem toggles = [state toggles x y | x <- [0..2], y <- [0..2]]

state toggles x y = let
  count = toggleCount toggles x y
  in if even count then '1' else '0'

toggleCount toggles x y = toggleTimes toggles (x - 1) y
  + toggleTimes toggles x y
  + toggleTimes toggles (x + 1) y
  + toggleTimes toggles x (y - 1)
  + toggleTimes toggles x (y + 1)

toggleTimes toggles x y = if isValid x y then toggles !! x !! y else 0

isValid x y = x >= 0 && x < 3 && y >= 0 && y < 3

import Data.List
import Control.Monad
import qualified Data.Text as T

(f3, x, o, os )  = ((\a b c -> a . b . c), not, id, replicate 9 o)

to01 = map T.unpack . T.chunksOf 3 . T.pack .  map (\x -> if x then '1' else '0')

solve [a,b,c,d,e,f,g,h,i] =
    zipWith ($)
        (zipWith3 f3
            (zipWith3 f3 (if a==1 then [x,x,o,x,o,o,o,o,o] else os)
                         (if b==1 then [x,x,x,o,x,o,o,o,o] else os)
                         (if c==1 then [o,x,x,o,o,x,o,o,o] else os))
            (zipWith3 f3 (if d==1 then [x,o,o,x,x,o,x,o,o] else os)
                         (if e==1 then [o,x,o,x,x,x,o,x,o] else os)
                         (if f==1 then [o,o,x,o,x,x,o,o,x] else os))
            (zipWith3 f3 (if g==1 then [o,o,o,x,x,o,x,o,o] else os)
                         (if h==1 then [o,o,o,x,x,x,o,x,o] else os)
                         (if i==1 then [o,o,o,o,o,x,o,x,x] else os)))
        $ replicate 9 True

main = do
    a <- concat <$> replicateM 3 (map(read :: String -> Int) . words <$>  getLine)
    mapM_ putStrLn $ to01 $ solve $ map (`mod` 2) a

import Data.List
import Control.Monad
import qualified Data.Text as T
import Data.Maybe

(f3, x, o, os, zeroes )  = ((\a b c -> a . b . c), not, id, replicate 9 o, replicate 9 0)

to01 = map T.unpack . T.chunksOf 3 . T.pack .  map (\x -> if x then '1' else '0')

solve [a,b,c,d,e,f,g,h,i] js =
    zipWith ($)
        (zipWith3 f3
            (zipWith3 f3 (if a==1 then [x,x,o,x,o,o,o,o,o] else os)
                         (if b==1 then [x,x,x,o,x,o,o,o,o] else os)
                         (if c==1 then [o,x,x,o,o,x,o,o,o] else os))
            (zipWith3 f3 (if d==1 then [x,o,o,x,x,o,x,o,o] else os)
                         (if e==1 then [o,x,o,x,x,x,o,x,o] else os)
                         (if f==1 then [o,o,x,o,x,x,o,o,x] else os))
            (zipWith3 f3 (if g==1 then [o,o,o,x,x,o,x,o,o] else os)
                         (if h==1 then [o,o,o,x,x,x,o,x,o] else os)
                         (if i==1 then [o,o,o,o,o,x,o,x,x] else os)))
        js

main = do
    a <- concat <$> replicateM 3 (map(read :: String -> Int) . words <$>  getLine)
    mapM_ putStrLn $ to01 $ fst $ until ((==zeroes).snd) (\(x,y) -> let b = findIndex (/=0) y in if isNothing b then (x,y) else let z = take 9 $ replicate (fromJust b) 0 ++ [1] ++ repeat 0 in (solve z x, zipWith (-) y z)) (replicate 9 True, a)

import Control.Applicative
import Data.List
import Data.Array

q::[[Int]]
q=[[1,2,4],[1,2,3,5],[2,3,6],[1,4,7,5],[2,4,5,6,8],[3,5,6,9],[7,8,4],[8,7,9,5],[9,8,6]]

process::[Int]->Array Int Int->Int->[Int]
process [] a _ = elems a
process (x:xs) a n | even x = process xs a (n+1)
                   | otherwise = process xs (a//(map (\z->(z,1-a!z)) (q!!n))) (n+1)

chunks n [] = []
chunks n x = (take n x ): (chunks n (drop n x))

main= do
	x<- map read. words <$> getContents::IO [Int]
	let b= listArray (1,9) [1,1,1,1,1,1,1,1,1]
	putStrLn $ unlines $ map unwords $  chunks 3 $ map show $ process x  b 0


main = interact ((++"\n") . show . solve . lines)

solve [s1, s2] =
      let an1 = 0 :(annotate s1 s2)
          an2 = reverse $ annotate (reverse s1) (reverse s2)
          an2' =  an2
      in find_opt an1 an2' (length s2)

annotate s t = (annotate' s t 0)

annotate' [] [] n = []
annotate' s  [] n = n : (annotate' (tail s) [] n)
annotate' (sx:sr) (tx:tr) n
  | sx == tx = (n + 1) : (annotate' sr tr (n + 1))
  | otherwise = n : (annotate' sr (tx:tr) n)

-- find_opt a1 a2 n = a2
find_opt a1 a2 n = find_opt' a1 a2 n 0

find_opt' (ax:_) [] n diff
  | ax >= n = diff
  | otherwise = diff - 1
find_opt' (a1x:a1r) (a2x:a2r) n diff
  | a1x + a2x >= n = max diff $ find_opt' (a1x:a1r) a2r n (diff + 1)
  | otherwise = find_opt' a1r (a2x:a2r) n (diff - 1)

import Control.Monad
import Data.List

readInt :: IO(Int)
readInt = fmap read getLine

readlist :: IO([Integer])
readlist = fmap (map read . words) getLine

loop :: Int -> IO() -> IO()
loop 0 f = do
    f; loop 0 f
loop 1 f = f
loop x f = do
    f; loop (x - 1) f

main = do
	s <- getLine
	t <- getLine
	print (solve s t)

solve :: String -> String -> Int
solve s t = (foldl1 max . zipWith (-) rv $ v) - 1
	where
		v  = (-1):(match (+) s t 0)
		rv = reverse v'
			where v' = (length s):(match (-) (reverse s) (reverse t) $ (length s - 1))

match :: (Int -> Int -> Int) -> String -> String -> Int -> [Int]
match _ _ [] _ = []
match f (x0:xs) y@(y0:ys) pos =
	if x0 == y0 then
		(pos:(match f xs ys (pos `f` 1)))
	else
		match f xs y (pos `f` 1)


doit :: Integer -> Integer -> [Integer]
doit p k = map snd . takeWhile (/= (0, 0)) . iterate (f . fst) $ (p, 0)
  where f x = let ans = x `mod` k
              in ((x - ans) `div` (-k), ans)

main = do
  [p, k] <- fmap (fmap read . words) getLine
  let ans = drop 1 . doit p $ k
  print . length $ ans
  putStrLn . unwords . map show $ ans

doit :: Integer -> Integer -> [Integer]
doit 0 _ = []
doit p k = tmp : doit ((p - tmp) `div` (-k)) k
  where tmp = p `mod` k

main = do
  [p, k] <- fmap (fmap read . words) getLine
  let ans = doit p k
  print . length $ ans
  putStrLn . unwords . map show $ ans

doit :: [Integer] -> [Integer]
doit [0, _] = []
doit [p, k] = tmp : doit [(p - tmp) `div` (-k), k]
  where tmp = p `mod` k

main = fmap (fmap read . words) getLine >>= return . doit >>= putStrLn . unwords . map show


import Monad (liftM)
import Prelude hiding (print)

solve :: (Fractional a, Ord a) => [a] -> (a, a)
solve [a, b, m, vx, vy, vz]  =  solve' (a/2, m, 0) (vx, vy, vz)
  where
    solve' (x, y, z) (vx, vy, vz)
      | ty <= tx && ty <= tz  =  (x + ty * vx, z + ty * vz)
      | tx <  tz   =  solve' (x + tx * vx, y + tx * vy, z + tx * vz) (-vx, vy, vz)
      | tx >  tz   =  solve' (x + tz * vx, y + tz * vy, z + tz * vz) (vx, vy, -vz)
      | otherwise  =  solve' (x + tz * vx, y + tz * vy, z + tz * vz) (-vx, vy, -vz)
      where
        tx  =  if vx >= 0 then (a - x) / vx else -x / vx
        ty  =  -y / vy
        tz  =  if vz >= 0 then (b - z) / vz else -z / vz

print :: Show a => (a, a) -> IO ()
print (a, b)  =  putStrLn (show a ++ " " ++ show b)

main :: IO ()
main  =  fmap (map read . take 6 . words) getContents >>= print . solve

import Control.Applicative

main = do
  [a,b,m] <- map read.words <$> getLine
  [vx,vy',vz] <- map read.words <$> getLine
  let vy = -vy'
      qx = (2*vx*m+a*vy)`div`(2*vy*a)
      qz = (vz*m)`div`(vy*b)
      x0 = if even qx
              then (fromIntegral $ -2*vy*qx*a+2*vx*m+a*vy)/(fromIntegral $ 2*vy)
              else (fromIntegral $ 2*vy*(qx+1)*a-2*vx*m-a*vy)/(fromIntegral $ 2*vy)
      z0 = if even qz
              then (fromIntegral $ vz*m-qz*b*vy)/(fromIntegral vy)
              else (fromIntegral $ -vz*m+(qz+1)*b*vy)/(fromIntegral vy)
  putStrLn $ unwords $ map show [x0,z0]

import Monad (liftM)
import Prelude hiding (print)

solve :: (Fractional a, Ord a) => [a] -> (a, a)
solve [a, b, m, vx, vy, vz]  =  solve' (a/2, m, 0) (vx, vy, vz)
  where
    solve' (x, y, z) (vx, vy, vz)
      | ty <= tx && ty <= tz  =  (x + ty * vx, z + ty * vz)
      | tx <= tz   =  solve' (0, y + tx * vy, z + tx * vz) (-vx, vy, vz)
      | otherwise  =  solve' (x + tz * vx, y + tz * vy, 0) (vx, vy, -vz)
      where
        tx  =  if vx > 0 then (a - x) / vx else x / vx
        ty  =  -y / vy
        tz  =  if vz > 0 then (b - z) / vz else z / vz

print :: Show a => (a, a) -> IO ()
print (a, b)  =  putStrLn (show a ++ " " ++ show b)

main :: IO ()
main  =  fmap (map read . take 6 . words) getContents >>= print . solve


main = interact (show . (solve 1) . read)
solve a b
    | b <= a    = b
    | otherwise = solve (a+1) (b-a)

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
import           Control.Applicative
import           Control.Monad
import           Data.List
import           Data.Tuple
import           Data.Int
import           Data.Char
import           Data.Function
-- import           Data.ByteString (ByteString)
-- import qualified Data.ByteString.Char8 as C
-- import           Data.ByteString.Char8 ()
-- import           Data.Vector.Unboxed ((//), (++), (!), (!?))
-- import qualified Data.Vector.Unboxed as U
-- import           Data.IntMap.Strict (IntMap)
-- import qualified Data.IntMap.Strict as IntMap
-- import           Data.Sequence ((|>), (<|), (><),ViewR((:>)), ViewL((:<)))
-- import qualified Data.Sequence as S

myread = read :: String -> Integer
main = do
  n <- myread <$> getLine
  print $ solve n

solve :: Integer -> Integer
solve n = n - ((m*(m+1))`div`2) where
  m = ($!) floor ((sqrt (((fromIntegral n -1) * 8)+1) - 1) / 2)
-- solve n = (-) n . last . takeWhile (<n) $ [sum [1..n] | n <- [1..]]

import Control.Applicative
import Control.Monad
import Data.List
import Data.Ord

main= do
	n<- read <$> getLine::IO Integer
	let x = floor  $ sqrt $ fromIntegral $ n*2
	let y= (div (x*(x+1)) 2)
	let x1 = if (n>y) then   n-y else   n- (div ((x)*(x-1)) 2)
	let x2 = if x1 ==0 then  x else   x1
	print x2

main = interact $ show . sol . map read . words

sol :: [Integer] -> Integer
sol [n] = f n 1
    where
    f num p
        | num > p = f (num-p) (p+1)
        | otherwise = num

{-# OPTIONS_GHC -O2 #-}

import Data.Int (Int64)

main = do
  a <- readLn :: IO Int64

  let
    b = f a 1
      where
        f a d = if a <= d then a else f (a-d) (d+1)

  print b

main = do
       e<-getLine
       let m=read e::Integer
           n=read e::Double
           r=((-1.0)+sqrt(1+8*n))/2.0
           r2=floor r::Integer
           r3=ceiling r::Integer
           r4=if r3==r2 then (r2-1) else r2
           ans=m - (r4*(r4+1::Integer) `div` 2)
       print ans


import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.List
import Control.Arrow

int = fst . fromJust . B.readInt
ints = map int . B.words <$> B.getLine
g = (arr length *** arr length) .  partition (<0) .filter (/=0)
sub (a,b) (c,d) = (a-c, b-d)

main = do
    [n,k] <- ints
    bs <- ints
    let gbs = g bs
    print $ maximum $ map (\x -> abs $ uncurry (-) $ sub gbs $ g $ zipWith (*) bs $ take n $ drop x $ cycle $ replicate (k-1) 0 ++ [1]) [0..k-1]

import Data.List

main = getContents >>= print . solve . map read . tail . words

-- find maximum |e - s| in groups
solve (k:xs) = maximum $ map (\(e,s) -> abs $ e0 - e - s0 + s) xss
    where xss = map tab $ transpose $ groupK k xs
          (e0,s0) = tab xs

groupK k [] = []
groupK k xs = (\(l,rs) -> l : groupK k rs) $ splitAt k xs

-- (# of tab 1, # of tab -1)
tab xs = let (n,s) = (length xs, sum xs) in ((n + s) `div` 2, (n - s) `div` 2)

-- Codeforces Round #532 (A), Contest 1100, Problem set ?.
-- UNDONE

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word
import Data.STRef
import Data.IORef
import Data.Function

main :: IO ()
main = do
  [n,k] <- map read . words <$> getLine :: IO [Int]
  ts <- unfoldr (BS.readInt . BS.dropWhile (<'!')) <$> BS.getLine
  let s = sum ts
  print $ maximum $ map abs $ map (s-) $ A.elems
    (A.accumArray (+) 0 (0,k-1) $ zip (cycle [0..k-1]) ts :: UArray Int Int)

import Text.Printf
import Data.List
import Data.Functor
import Control.Applicative
import Prelude
import Control.Monad
import Data.Maybe
import Data.Function
import Data.Array
import qualified Data.ByteString.Char8 as B8
import Data.Foldable (foldr')
import Data.Bits

readInt :: String -> Int
readInt = Prelude.read

readB8Int :: B8.ByteString -> Int
readB8Int = fst . fromJust . B8.readInt

main :: IO ()
main = do
    [n, k] <- (map readInt . words) <$> getLine
    arr <- (map readInt . words) <$> getLine
    let len = length arr
        sm = sum arr
        ss = map (\b -> abs (sm - (sum . map (arr !!) . map (\x -> x - 1) . takeWhile (<= len) . map (\ i -> i * k + b) $ [0 .. ]))) $ [1 .. k]
    printf "%d\n" $ maximum ss


main = readLn >>= print . (`mod` 2) . (`div` 2) . (+1)

-- Haskell Template
solve :: Int -> Int
solve n
  | r == 0 || r == 3 = 0
  | otherwise = 1
  where
  	r = mod n 4

main :: IO ()
main = do
  x <- getLine
  let s = solve (read x)
  putStrLn (show s)

-- Codeforces Round #531 (A), Contest 1102, Problem set ?.

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE TupleSections#-}
{-# LANGUAGE ExplicitForAll #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IM
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
-- import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
{-
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
-}
import Control.Monad
import Control.Monad.ST.Safe
-- import Control.Monad.ST
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import Data.Word

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  print $ if mod n 4 `elem` [0,3] then 0 else 1

main = do
    line <- getLine
    let n = read line :: Int
    let m = case n `mod` 4 of 0 -> 0
                              1 -> 1
                              2 -> 1
                              3 -> 0
    print m

main = interact $ show . solve . read
solve n = n * (n+1) `div` 2 `mod` 2

import Control.Applicative
import Control.Monad
import Data.List

main = do
		r<- read <$> getLine ::IO Int
		print $ if odd (div (r *(r+1)) 2) then 1 else 0

main :: IO ()
main = do
  l <- readLn :: IO Int
  putStrLn $ show $ solve l

solve :: Int -> Int
solve 0 = 0
solve 1 = 1
solve 2 = 1
solve 3 = 0
solve n = solve $ n `mod` 4


{-# LANGUAGE LambdaCase #-}
-- import Control.Arrow
import Data.List
import Data.Ord
import Data.Monoid

main = do
    abc <- map (read :: String -> Int) . words <$> getLine
    let d = take 14 $ cycle "abcacba"
        p s t =  (\case
                    x@[('a':_),('b':_),('c':_)] -> map length x
                    [as@('a':_),bs@('b':_)] -> [length as, length bs, 0]
                    [as@('a':_),cs@('c':_)] -> [length as, 0, length cs]
                    [bs@('b':_),cs@('c':_)] -> [0, length bs, length cs]
                    [as@('a':_)] -> [length as, 0, 0]
                    [bs@('b':_)] -> [0,length bs, 0]
                    [cs@('c':_)] -> [0, 0, length cs]
                ) . group . sort . take t . drop s
        ps = [(t,p s t d) | s <- [0..6], t <- [1..7]]
        zs'@[(ad,ar),(bd,br),(cd,cr)] = zipWith divMod abc [3,2,2]
        wk = minimum $ map fst zs'
        zs = zipWith min [3,2,2] $ zipWith (-) abc $ map (wk*) [3,2,2]
        ys = if zs == [0,0,0]
            then [(7*wk, zs)]
            else filter (all (>=0) . snd) $ map (\x -> (7*wk + fst x, zipWith (-) zs $ snd x)) ps
    print $ fst $ head $  sortBy ((comparing (sum .snd)) `mappend` (flip $ comparing fst)) ys

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ maximum (map (h a b c) [0..6])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
dayz :: (Maybe (Int, (Int, Int, Int))) -> Int
dayz Nothing = (-1)
dayz (Just (d,(a,b,c)))
    |a>21000 && b>14000 && c>14000 = 49000+ (dayz $ Just(d,(a-21000,b-14000,c-14000)))
    |otherwise = 1 + (dayz (step (d,(a,b,c))))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok ((d+1) `mod` 7,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if -1 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

dine a b c week = case (head week) of
  'a' -> if a == 0 then 0 else 1 + (dine (a - 1) b c (tail week))
  'b' -> if b == 0 then 0 else 1 + (dine a (b - 1) c (tail week))
  'c' -> if c == 0 then 0 else 1 + (dine a b (c - 1) (tail week))
process _ _ _ 0 _ = 0
process a b c n week = max (dine a b c week) rec
  where
    rec = process a b c (n - 1) (tail week)
analyze (a:b:c:_) = 7 * n + (process (a - n * 3) (b - n * 2) (c - n * 2) 7 week)
  where
    n = minimum [div a 3, div b 2, div c 2]
    week = cycle ['a', 'b', 'c', 'a', 'c', 'b', 'a']
ans = show . analyze . (map read) . words
main = getLine >>= putStrLn . ans

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ maximum (map (h a b c) [0..7])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
--days :: {-Maybe (Int, (Int, Int, Int))-} Maybe a -> Int
dayz Nothing = (-1)
dayz (Just x) = 1 + (dayz (step x))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok (d+1,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if -1 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ maximum (map (h a b c) [0..7])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
dayz :: (Maybe (Int, (Int, Int, Int))) -> Int
dayz Nothing = (-1)
dayz (Just (d,(a,b,c)))
    |a>21000 && b>14000 && c>14000 = 49000+ (dayz $ Just(d,(a-21000,b-14000,c-14000)))
    |otherwise = 1 + (dayz (step (d,(a,b,c))))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok ((d+1) `mod` 7,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if -1 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ {-maximum-} (map (h a b c) [0..7])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
--days :: {-Maybe (Int, (Int, Int, Int))-} Maybe a -> Int
dayz Nothing = (-1)
dayz (Just x) = 1 + (dayz (step x))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok (d+1,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if -1 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ {-maximum-} (map (h a b c) [0..7])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
--days :: {-Maybe (Int, (Int, Int, Int))-} Maybe a -> Int
dayz Nothing = 0
dayz (Just x) = 1 + (dayz (step x))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok (d+1,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if 0 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

main :: IO ()
main = do
    inp <- getLine
    let l = map read $ words inp
        a = l!!0
        b = l!!1
        c = l!!2
    putStrLn $ show $ maximum (map (h a b c) [0..7])
    return ()

h :: Int -> Int -> Int -> Int -> Int
h a b c d = dayz $ Just (d,(a,b,c))
--days :: {-Maybe (Int, (Int, Int, Int))-} Maybe a -> Int
dayz Nothing = 0
dayz (Just x) = 1 + (dayz (step x))

step :: (Int,(Int,Int,Int))-> Maybe (Int,(Int,Int,Int))
step (d,(a,b,c))
    |d `elem` [0,3,6] =ok (d+1,(a-1,b,c))
    |d `elem` [1,5]   =ok (d+1,(a,b-1,c))
    |otherwise        =ok ((d+1) `mod` 7,(a,b,c-1))
    where ok (a,(b,c,d)) = if 0 `elem` [b,c,d] then Nothing else Just (a,(b,c,d))

{-# LANGUAGE LambdaCase #-}
-- import Control.Arrow
import Data.List
import Data.Ord
import Data.Monoid

main = do
    abc <- map (read :: String -> Int) . words <$> getLine
    let d = take 14 $ cycle "abcacba"
        p s t =  (\case
                    x@[('a':_),('b':_),('c':_)] -> map length x
                    [as@('a':_),bs@('b':_)] -> [length as, length bs, 0]
                    [as@('a':_),cs@('c':_)] -> [length as, 0, length cs]
                    [bs@('b':_),cs@('c':_)] -> [0, length bs, length cs]
                    [as@('a':_)] -> [length as, 0, 0]
                    [bs@('b':_)] -> [0,length bs, 0]
                    [cs@('c':_)] -> [0, 0, length cs]
                ) . group . sort . take t . drop s
        ps = [(t,p s t d) | s <- [0..6], t <- [1..7]]
        zs'@[(ad,ar),(bd,br),(cd,cr)] = zipWith divMod abc [3,2,2]
        wk = minimum $ map fst zs'
        zs = zipWith (-) abc $ map (wk*) [3,2,2]
        ys = if zs == [0,0,0]
            then [(7*wk, zs)]
            else map (\x -> (7*wk + fst x, zipWith (\x y -> if x < y then x else x -y) zs $ snd x)) ps
    print $ fst $ head $  sortBy ((flip $ comparing (length . filter (==0) .snd)) `mappend` (flip $ comparing fst)) ys

{-# LANGUAGE LambdaCase #-}
-- import Control.Arrow
import Data.List
import Data.Ord
import Data.Monoid

main = do
    abc <- map (read :: String -> Int) . words <$> getLine
    let d = take 14 $ cycle "abcacba"
        p s t =  (\case
                    x@[('a':_),('b':_),('c':_)] -> map length x
                    [as@('a':_),bs@('b':_)] -> [length as, length bs, 0]
                    [as@('a':_),cs@('c':_)] -> [length as, 0, length cs]
                    [bs@('b':_),cs@('c':_)] -> [0, length bs, length cs]
                    [as@('a':_)] -> [length as, 0, 0]
                    [bs@('b':_)] -> [0,length bs, 0]
                    [cs@('c':_)] -> [0, 0, length cs]
                ) . group . sort . take t . drop s
        ps = [(t,p s t d) | s <- [0..6], t <- [1..7]]
        zs'@[(ad,ar),(bd,br),(cd,cr)] = zipWith divMod abc [3,2,2]
        wk = minimum $ map fst zs'
        zs = zipWith (-) abc $ map (wk*) [3,2,2]
        ys = if zs == [0,0,0]
            then [(7*wk, zs)]
            else map (\x -> (7*wk + fst x, map abs $ zipWith (-) zs $ snd x)) ps
    print $ fst $ head $  sortBy ((flip $ comparing (length . filter (==0) .snd)) `mappend` (flip $ comparing fst)) ys

{-# LANGUAGE LambdaCase #-}
-- import Control.Arrow
import Data.List
import Data.Ord
import Data.Monoid

main = do
   abc <- map (read :: String -> Int) . words <$> getLine
   let  d = take 14 $ cycle "abcacba"
        p s t =  (\case
                    x@[('a':_),('b':_),('c':_)] -> map length x
                    [as@('a':_),bs@('b':_)] -> [length as, length bs, 0]
                    [as@('a':_),cs@('c':_)] -> [length as, 0, length cs]
                    [bs@('b':_),cs@('c':_)] -> [0, length bs, length cs]
                    [as@('a':_)] -> [length as, 0, 0]
                    [bs@('b':_)] -> [0,length bs, 0]
                    [cs@('c':_)] -> [0, 0, length cs]
                ) . group . sort . take t . drop s
        ps = [(t,p s t d) | s <- [0..6], t <- [1..7]]
        zs'@[(ad,ar),(bd,br),(cd,cr)] = zipWith divMod abc [3,2,2]
        wk = minimum $ map fst zs'
        zs = zipWith (-) abc $ map (wk*) [3,2,2]
        ys = map (\x -> (7*wk + fst x, map  abs $ zipWith (-) zs $ snd x)) ps
   print $ fst $ head $ sortBy ((flip $ comparing (length . filter (==0) .snd)) `mappend` (flip $ comparing fst)) ys

{-# LANGUAGE LambdaCase #-}
-- import Control.Arrow
import Data.List
import Data.Ord
import Data.Monoid

main = do
    abc <- map (read :: String -> Int) . words <$> getLine
    let d = take 14 $ cycle "abcacba"
        p s t =  (\case
                    x@[('a':_),('b':_),('c':_)] -> map length x
                    [as@('a':_),bs@('b':_)] -> [length as, length bs, 0]
                    [as@('a':_),cs@('c':_)] -> [length as, 0, length cs]
                    [bs@('b':_),cs@('c':_)] -> [0, length bs, length cs]
                    [as@('a':_)] -> [length as, 0, 0]
                    [bs@('b':_)] -> [0,length bs, 0]
                    [cs@('c':_)] -> [0, 0, length cs]
                ) . group . sort . take t . drop s
        ps = [(t,p s t d) | s <- [0..6], t <- [1..7]]
        zs'@[(ad,ar),(bd,br),(cd,cr)] = zipWith divMod abc [3,2,2]
        wk = minimum $ map fst zs'
        zs = zipWith (-) abc $ map (wk*) [3,2,2]
        ys = if zs == [0,0,0]
            then [(7*wk, zs)]
            else map (\x -> (7*wk + fst x, zipWith (\x y -> if x < y then x else x -y) zs $ snd x)) ps
    print zs
    print $ fst $ head $  sortBy ((flip $ comparing (length . filter (==0) .snd)) `mappend` (flip $ comparing fst)) ys


import Data.Char

solve [a,b]
      | b `div` a >= 3 = 0
      | otherwise = a - (b `mod` a)

main = interact $ show . solve . map (read::String->Int) . words

main = getLine >>= print. (\[n, k] -> max 0 (3*n - k)). map read. words

main=interact$show.(\[n,k] -> max (3*n-k) 0).map read.words

solve n k = n - min (k - 2*n) n
main = do
    line <- getLine
    let [n, k] = map read $ words line :: [Int]
    print $ solve n k

main = getLine >>= putStrLn . func . map read . words
func :: [Int] -> String
func [x,y] = show $ max 0 (3*x - y)

s[n,k]=max (3*n-k) 0
main=interact$show.s.map read.words

import Control.Monad (liftM)
import Prelude hiding (reads)

reads :: Read a => IO [a]
reads  =  liftM (map read . words) getLine

solve :: [Int] -> Int
solve [n, k]  =  max 0 (3*n - k)

main :: IO ()
main  =  reads >>= print . solve

solve :: Int -> Int -> Int
solve n k  =  minimum
  [n - n3 - n4 - n5 |
    n3 <- [0..n], n4 <- [0..n - n3], n5 <- [0..n - n3 - n4],
    n3 + n4 + n5 <= n, 2 * (n - n3 - n4 - n5) + 3 * n3 + 4 * n4 + 5 * n5 == k]

main :: IO ()
main  =  do
    [n, k]  <-  getLine >>= return . map read . words
    print $ solve n k

main = do
  (n:k:_) <- (map read . words) `fmap` getLine
  let rest = k - 2 * n
  print $ max (n - rest) 0

main = getLine >>= print.solve.map read.words

solve :: [Int] -> Int
solve [n,k] = max 0 $ 3*n-k


main = interact $ show . solve . map read . words
solve (n0:k0:as) = go n0 k0 0 as where
  go n k _ _ | k <= 0 = n0 - n
  go 0 _ _ _ = -1
  go _ _ _ [] = -1
  go n k c (a:as) = go (n-1) (k - g) (c' - g) as where
    c' = c + a
    g = min 8 c'

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
module Main where
import qualified Data.ByteString.Lazy.Char8 as B
import Control.Monad.Trans.State.Lazy
import Control.Monad

main =  mapM_ print . evalState app . B.words =<< B.getContents

app = do
  n <- poi
  k <- poi
  xs <- replicateM n poi
  return [solve n k xs]
  where
    pop = state $ \(x:xs) -> (x, xs)
    poi = fmap int pop
    int = maybe undefined fst . B.readInt

solve n k xs = (\r -> if r > n then -1 else r) $ length $ takeWhile ((< k) . fst) $ scanl (\(t, l) x -> let x' = x + l in (t + min x' 8, max 0 (x' - 8))) (0, 0) xs

f [x] n k s day
	|s+(min 8 x)>=k = day
	|otherwise = (-1)
f (x:y:xs) n k s day
	|s+(min 8 x)>=k = day
	|otherwise = f new_xs n k (s + (min x 8)) (day+1)
	where
		new_xs=(y+x-(min x 8)):xs

main=do
	a<-fmap words (getLine)
	b<-fmap words (getLine)
	let [n,k] = map (\x->read x::Int) a
	let aa = map (\x->read x::Int) b
	print (f aa n k 0 1)

doit :: (Int, Int) -> Int -> (Int, Int)
doit (rem, me) new =
  let menew = me + new
      give = 8 `min` menew
      in (rem - give, menew - give)

main = do
  [n, k] <- fmap (map read . words) getLine
  a <- fmap (map read . words) getLine
  let x = length . takeWhile ((> 0) . fst) . scanl doit (k, 0) $ a
  print $ if x == n + 1 then -1 else x

arya _ k _ p | k <= 0 = p
arya _ _ [] p = -1
arya n k [a] p = if k > 8 then -1 else if k > a then -1 else p+1
arya n k (a:b:as) p = arya n (if a > 8 then k - 8 else k - a) (if a > 8 then (a-8+b:as) else  (b:as)) (p+1)

main = do
 [n,k] <- getLine >>= return . map (\x -> read x :: Int) . words
 as <- getLine >>= return . map (\x -> read x :: Int) . words
 print $ arya n k as 0

f [x] n k s day
	|s+(min 8 x)>=k = day
	|otherwise = (-1)
f (x:y:xs) n k s day
	|s>=k = day
	|otherwise = f new_xs n k (s + (min x 8)) (day+1)
	where
		new_xs=(y+x-(min x 8)):xs

main=do
	a<-fmap words (getLine)
	b<-fmap words (getLine)
	let [n,k] = map (\x->read x::Int) a
	let aa = map (\x->read x::Int) b
	print (f aa n k 0 1)

f [] n k s
	|s>=k = n
	|otherwise = (-1)
f (x:xs) n k s
	|s>=k = n
	|otherwise = f xs n k (s + (min x 8))

main=do
	a<-fmap words (getLine)
	b<-fmap words (getLine)
	let [n,k] = map (\x->read x::Int) a
	let aa = map (\x->read x::Int) b
	print (f aa n k 0)

f [] n k s day
	|s>=k = day
	|otherwise = (-1)
f (x:xs) n k s day
	|s>=k = day
	|otherwise = f xs n k (s + (min x 8)) (day+1)

main=do
	a<-fmap words (getLine)
	b<-fmap words (getLine)
	let [n,k] = map (\x->read x::Int) a
	let aa = map (\x->read x::Int) b
	print (f aa n k 0 0)

arya _ k _ p | k <= 0 = p
arya _ _ [] p = -1
arya n k [a] p = -1
arya n k (a:b:as) p = arya n (if a > 8 then k - 8 else k - a) (if a > 8 then (a-8+b:as) else  (b:as)) (p+1)

main = do
 [n,k] <- getLine >>= return . map (\x -> read x :: Int) . words
 as <- getLine >>= return . map (\x -> read x :: Int) . words
 print $ arya n k as 0

arya _ _ [] p = if p == 0 then -1 else p
arya n k [a] p = if k > 8 then -1 else p+1
arya n k (a:b:as) p = arya n (if a > 8 then k - 8 else k - a) (if a > 8 then (a-8+b:as) else  (b:as)) (p+1)

main = do
 [n,k] <- getLine >>= return . map (\x -> read x :: Int) . words
 as <- getLine >>= return . map (\x -> read x :: Int) . words
 print $ arya n k as 0

arya _ k _ p | k <= 0 = p
arya _ _ [] p = -1
arya n k [a] p = if k > 8 then -1 else p+1
arya n k (a:b:as) p = arya n (if a > 8 then k - 8 else k - a) (if a > 8 then (a-8+b:as) else  (b:as)) (p+1)

main = do
 [n,k] <- getLine >>= return . map (\x -> read x :: Int) . words
 as <- getLine >>= return . map (\x -> read x :: Int) . words
 print $ arya n k as 0


convert '^'=1
convert '>'=2
convert 'v'=3
convert '<'=4
update s f
	|f>=s = (s,f)
	|otherwise=(s,f+4)
func s f time
	|abs (ss - ff )==0 = "undefined"
	|abs (ss - ff )==2 = "undefined"
	|ff-ss==1 && t==1 = "cw"
	|ff-ss==1 && t==3 = "ccw"
	|ff-ss==3 && t==1 = "ccw"
	|ff-ss==3 && t==3 = "cw"
	|otherwise = "undefined"
	where
		t=mod time 4
		(ss,ff)=update (convert s) (convert f)

main=do
	[s,f]<-fmap words getLine
	time<-fmap (\x->read x::Integer) getLine
	putStrLn (func (head s) (head f) time)

-- Try Codeforces
-- author: Leonardone @ NEETSDKASU

main = putStrLn . solve . words =<< getContents

solve [_  , _  ,"0"] = "undefined"
solve [s:_, e:_, n ] = ans where
    m = (read n - 1) `mod` 4
    cw  = (!! m) . tail $ dropWhile (/= s) "^>v<^>v<^>v<^>v<"
    ccw = (!! m) . tail $ dropWhile (/= s) "^<v>^<v>^<v>^<v>"
    ans = case (cw == e, ccw == e) of
        (True, False) -> "cw"
        (False, True) -> "ccw"
        otherwise     -> "undefined"

{-# OPTIONS_GHC -O2 -optc-O3 -optc-ffast-math -funfolding-use-threshold=16 -fexcess-precision -funbox-strict-fields #-}
module Main where
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.Bits
import Data.List

main = do
  [b, _, e] <- getLine
  n <- fmap readInt B.getLine
  putStr $ solve b e n

solve b e n
  | not $ x `xor` y = "undefined"
  | x = "cw"
  | y = "ccw"
  where
    x = cs!!(n' + fromJust (elemIndex b cs)) == e
    y = as!!(n' + fromJust (elemIndex b as)) == e
    cs = "v<^>v<^>"
    as = "v>^<v>^<"
    n' = n `mod` 4

readInt = maybe undefined fst . B.readInt
readInts = fmap readInt . B.words

convert '^'=1
convert '>'=2
convert 'v'=3
convert '<'=4
func s f time
	|t==0 = "undefined"
	|abs(ff-ss)==2 = "undefined"
	|ff>ss = "cw"
	|ff<ss = "ccw"
	|ff==ss="undefined"
	where
		t=mod time 4
		ff=convert f
		ss=convert s

main=do
	[s,f]<-fmap words getLine
	time<-fmap (\x->read x::Integer) getLine
	putStrLn (func (head s) (head f) time)

convert '^'=1
convert '>'=2
convert 'v'=3
convert '<'=4
updateT 0 = 4
updateT t = t
func s f time
	|t==0 = "undefined"
	|abs(ff-ss)==2 = "undefined"
	|ff>ss = "cw"
	|ff<ss = "ccw"
	|ff==ss="undefined"
	where
		t=updateT (mod time 4)
		ff=convert f
		ss=convert s

main=do
	[s,f]<-fmap words getLine
	time<-fmap (\x->read x::Integer) getLine
	putStrLn (func (head s) (head f) time)

convert '^'=1
convert '>'=2
convert 'v'=3
convert '<'=4
update s f
	|f>=s = (s,f)
	|otherwise=(s,f+4)
func s f time
	|abs (ss - ff )==0 = "undfined"
	|abs (ss - ff )==2 = "undfined"
	|ff-ss==1 && t==1 = "cw"
	|ff-ss==1 && t==3 = "ccw"
	|ff-ss==3 && t==1 = "ccw"
	|ff-ss==3 && t==3 = "cw"
	|otherwise = "undefined"
	where
		t=mod time 4
		(ss,ff)=update (convert s) (convert f)

main=do
	[s,f]<-fmap words getLine
	time<-fmap (\x->read x::Integer) getLine
	putStrLn (func (head s) (head f) time)