{-# LANGUAGE BlockArguments #-}

import Control.Arrow

main = do { x <- getLine; y <- getLine; putStrLn $ solve x (words y) }

canPlay :: String -> String -> String
canPlay x y | x!!0 == y!!0 || x!!1 == y!!1 = "YES"
            | x!!0 /= y!!0 && x!!1 /= y!!1 = "NO"

canPlayAgg :: String -> String -> String -> String
canPlayAgg a b "YES" = "YES"
canPlayAgg a b "NO" = canPlay a b

solve :: String -> [String] -> String
solve a b = foldr (canPlayAgg a) "NO" b

import Control.Monad

cardsForPlayer = 5
cardsForStartGame = cardsForPlayer + 1

getSuit = ["D", "C", "S", "H"]
getRank = ["2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"]

getAllPairs :: [String] -> [String] -> [String]
getAllPairs xs zs = liftM2 (++) xs zs

pickCards :: [Int] -> [String] -> [String]
pickCards pick cards = aggregateCards pick cards []
  where aggregateCards [] _ res = res
        aggregateCards (x:xs) cards res =
          let card = cards!!x
              cardsWithoutPicked = (take x cards) ++ (drop (x+1) cards)
          in aggregateCards xs cardsWithoutPicked (card:res)

makeGameState :: [String] -> (String, [String])
makeGameState (x:xs) = (x, xs)

randomCards = [3, 5, 6, 12, 15, 40]

solve :: (String, [String]) -> String
solve (tableCard, (x:xs))
  | head tableCard == head x || last tableCard == last x = "YES"
  | xs == [] = "NO"
  | otherwise = solve (tableCard, xs)

getList :: IO [String]
getList = do
  line <- getLine
  return $ words line

main :: IO ()
main = do
   cardOnTable <- getLine
   playerCards <- getList
   putStrLn $ solve (cardOnTable, playerCards)

import Data.List
main = interact $ solve . lines
solve [t,h] | not (null (intersect t (concat (words h)))) = "YES"
            | otherwise = "NO"

import Control.Applicative
import Control.Monad
import Data.List
import Data.Maybe
import Data.Array
import qualified  Data.Set as S

main = do

	n1<-  S.fromList <$> getLine
	n2<-  S.fromList <$> getLine
	putStrLn  $ if S.disjoint n1 n2 then "NO" else "YES"

--ghc 7.10

isCompatible :: String -> String -> Bool
isCompatible (suit1:rank1:_) (suit2:rank2:_) = (suit1 == suit2) || (rank1 == rank2)
isCompatible _ _ = False

isPlay :: String -> [String] -> Bool
isPlay tCard hCards = any (isCompatible tCard) hCards

output = ["NO","YES"]

main = do
    tCard <- getLine
    line <- getLine
    let hCards = words line
    putStrLn $ output !! (fromEnum $ isPlay tCard hCards)

solve t cs  | xs /= [] || ys /= []  = "YES"
            | otherwise             = "NO"
            where
                xs = filter (==head t) [head c | c <- cs]
                ys = filter (==last t) [last c | c <- cs]

main = do
    t <- getLine
    line <- getLine
    let cs = words line
    putStrLn $ solve t cs

import Data.Set

main = do
  n <- getLine
  l <- fmap (Prelude.map show) getLine :: IO [String]
  let s = fromList l
  putStrLn( if (Data.Set.member (show $ (head n)) s) || (Data.Set.member (show $ (last n)) s) then "YES" else "NO" )

{-# LANGUAGE Strict #-}

module Main where

import           Data.List (elem)
import           System.IO

some :: String -> [String] -> Bool
some _ [] = False
some oneCard (x:xs) = elem (head oneCard) x || elem (last oneCard) x || some oneCard xs

main :: IO ()
main = do
  oneCard <- getLine
  myCards <- getLine
  let myCard = words myCards
  case some oneCard myCard of
    True -> putStrLn "YES"
    _    -> putStrLn "NO"

import System.IO (readFile, getLine)
import Data.Char (digitToInt)
import Data.List (intercalate)

data Suite = D | C | S | H deriving (Show, Eq)
data Rank = Rank Int deriving (Show, Eq, Ord)
data Card = Card Suite Rank deriving (Show, Eq)

checkCard :: Card -> Card -> Bool
checkCard (Card s r) (Card s2 r2) | s == s2 = True
                                  | r == r2 = True
                                  | otherwise = False

rankFromChar c | c == 'T' = Rank 10
               | c == 'J' = Rank 11
               | c == 'Q' = Rank 12
               | c == 'K' = Rank 13
               | c == 'A' = Rank 14
               | otherwise = Rank (digitToInt c)

suiteFromChar c | c == 'D' = D
                | c == 'C' = C
                | c == 'S' = S
                | c == 'H' = H

cardFromString s = let second x = head (tail x) in Card (suiteFromChar $ second s) (rankFromChar $ head s)

cardsFromString s = map cardFromString (words s)

calculate :: Card -> [Card] -> Bool
calculate tbl hold = any (checkCard tbl) hold

main = do
    tbl_s <- getLine
    hold_s <- getLine
    case calculate (cardFromString tbl_s) (cardsFromString hold_s) of
        True -> putStrLn "Yes"
        False -> putStrLn "No"

main = getContents >>= putStrLn . solve . words

solve ([x1,x2]:s)
    | any (\[s1,s2] -> x1 == s1 || x2 == s2) s = "YES"
    | otherwise = "NO"

import Control.Monad

main = getTwoLines >>= putStrLn . canPlay

getTwoLines :: IO (String, String)
getTwoLines = liftM2 toTuple getLine getLine
  where toTuple a b = (a, b)

canPlay :: (String, String) -> String
canPlay ([tRank, tSuit], hand) =
  case any canPlay' cards of
    True -> "yes"
    _ -> "no"
  where cards = words hand
        canPlay' [hRank, hSuit] = tRank == hRank || tSuit == hSuit

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

main :: IO ()
main = do
    myCard@[myRank, mySuit] <- getLine
    cards <- words <$> getLine
    let answer = any (\[rank, suit] -> rank == myRank || suit == mySuit) cards
    printf "%s" $ if answer then "YES" else "NO"

import Data.Set

main = do
  n <- getLine
  l <- fmap (Prelude.map show) getLine :: IO [String]
  let s = fromList l
  putStrLn( show $ if (Data.Set.member (show $ (head n)) s) || (Data.Set.member (show $ (last n)) s) then "YES" else "NO" )

{-# LANGUAGE Strict #-}

module Main where

import           Data.List (elem)
import           System.IO

some :: String -> [String] -> Bool
some _ [] = False
some oneCard (x:xs) = elem (head oneCard) x || elem (last oneCard) x || some oneCard xs

main :: IO ()
main = do
  oneCard <- getLine
  myCards <- getLine
  let myCard = words myCards
  case some oneCard myCard of
    True -> print "YES"
    _    -> print "NO"


import Control.Monad (replicateM)
import Data.Int (Int64)

main = do
  [n, p] <- fmap (map read . words) getLine
  hs <- replicateM n getLine

  let
    p' = fromIntegral p :: Int64
    count n [] = p'*n `div` 2
    count n (h:hs) =
      (p'*n `div` 2) + if h == "half" then count (2*n) hs else count (2*n+1) hs

  print $ count (1 :: Int64) $ tail $ reverse hs

half (a, b) p = (2 * a, b + a * p)
halfplus (a, b) p = (2 * a + 1, b + a * p + div p 2)

solve [] p = (0, 0)
solve (x:xs) p
    | (x == "half") = half (solve xs p) p
    | otherwise = halfplus (solve xs p) p

main = do
    content <- getContents
    let w = words content
    let p = read (w !! 1) :: Integer
    let res = solve (drop 2 w) p
    putStrLn . show . snd $ res

import Data.List

data Divisional = Divisional {numerator :: Integer, denominator :: Integer}
                deriving (Show)

readInteger :: String -> Integer
readInteger s = read s :: Integer

readBuyer :: Integer -> [String] -> IO([String])
readBuyer 0 buyers = return buyers
readBuyer a buyers = do
    buyer <- getLine
    readBuyer (a - 1) (buyer:buyers)

traceApple :: Integer -> Integer
traceApple a = 2 * a

tracePApple :: Integer -> Integer
tracePApple a = (2 * a) + 1

traceMoney :: Integer -> Integer -> Integer -> Integer
traceMoney money apple price = money + (apple * price * 2)

tracePMoney :: Integer -> Integer -> Integer -> Integer
tracePMoney money apple price = money + (apple * price * 2 + price)

solution :: Integer -> Integer -> Integer -> [String] -> Integer
solution _ currentMoney _ [] = currentMoney
solution currentApple currentMoney p ("halfplus":xs) = solution (tracePApple currentApple) (tracePMoney currentMoney currentApple p) p xs
solution currentApple currentMoney p ("half":xs) = solution (traceApple currentApple) (traceMoney currentMoney currentApple p) p xs

main :: IO()
main = do
    args <- getLine
    let (n:p:[]) = readInteger `map` (words args)
    buyers <- readBuyer (n - 1) []
    putStrLn $ show $ solution 1 (div p 2) (div p 2) buyers

{-# 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.Array
-- 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 -> Int
toint64 = fromIntegral :: Int -> Int64
main = do
  [n,p] <- map myread.words <$> getLine
  ns <- replicateM n getLine
  print $ solve (toint64 p) ns

solve :: Int64 -> [String] -> Int64
solve p ns = p`div`2 * go 0 0 (map ((==4).length) $ reverse ns) where
  go :: Int64 -> Int64 -> [Bool] -> Int64
  go v w [] = w
  go v w (True:xs) = go (v*2) (w+(v*2)) xs
  go v w (False:xs) = go (v*2+1) (w+(v*2+1)) xs

half (a, b) p = (2 * a, b + a * p)
halfplus (a, b) p = (2 * a + 1, b + a * p + div p 2)

solve [] p = (0, 0)
solve (x:xs) p
    | (x == "half") = half (solve xs p) p
    | otherwise = halfplus (solve xs p) p

main = do
    content <- getContents
    let w = words content
    let p = read (w !! 1) :: Int
    let res = solve (drop 2 w) p
    putStrLn . show . snd $ res

import Control.Monad (replicateM)

main = do
  [n, p] <- fmap (map read . words) getLine
  hs <- replicateM n getLine

  let
    count n [] = p*n `div` 2
    count n (h:hs) =
      (p*n `div` 2) + if h == "half" then count (2*n) hs else count (2*n+1) hs

  print $ count 1 $ tail $ reverse hs

import Data.List

data Divisional = Divisional {numerator :: Integer, denominator :: Integer}
                deriving (Show)

readInteger :: String -> Integer
readInteger s = read s :: Integer

readBuyer :: Integer -> [String] -> IO([String])
readBuyer 0 buyers = return buyers
readBuyer a buyers = do
    buyer <- getLine
    readBuyer (a - 1) (buyer:buyers)

traceApple :: Integer -> Integer
traceApple a = 2 * a

tracePApple :: Integer -> Integer
tracePApple a = (2 * a) + 1

traceMoney :: Integer -> Integer -> Integer -> Integer
traceMoney money apple price = money + (apple * price * 2)

tracePMoney :: Integer -> Integer -> Integer -> Integer
-- tracePMoney money apple price = money + (price * (apple * 4)) - price
tracePMoney money apple price = result
    where actualPrice = price * 2
          appleBought = apple * 2
          bonus = price
          result = actualPrice * appleBought - bonus

solution :: Integer -> Integer -> Integer -> [String] -> Integer
solution _ currentMoney _ [] = currentMoney
solution currentApple currentMoney p ("halfplus":xs) = solution (tracePApple currentApple) (tracePMoney currentMoney currentApple p) p xs
solution currentApple currentMoney p ("half":xs) = solution (traceApple currentApple) (traceMoney currentMoney currentApple p) p xs

main :: IO()
main = do
    args <- getLine
    let (n:p:[]) = readInteger `map` (words args)
    buyers <- readBuyer (n - 1) []
    putStrLn $ show $ solution 1 (div p 2) (div p 2) buyers


-- Codeforces 121A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = 2 + Data.List.sum (map (2 ^) [1..9])

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 444444444 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

import Data.List

luckyList = [4, 7] ++ concat [ [x*10+4, x*10+7] | x <- luckyList ]

solve :: [Integer] -> Integer
solve [l,r] = sum [ y * z | [x, y] <- luckyLinked, let z = max 0 $ min y r - max x (l-1) ]
   where luckyLinked = ([0,4]:) . map (take 2) . takeWhile ((<r) . head) $ tails luckyList

main = interact $(++"\n") .  show . solve . map read . words

-- vim: set expandtab:

{-
import HUnit

testInput :: Test
testInput  =  TestList "TestInput"
  [
    Test "#1" $ assertEq 33 $ solve 2 7,
    Test "#2" $ assertEq 7 $ solve 7 7
  ]

testNext :: Test
testNext  =  TestList "TestNext"
  [
    Test "#1" $ assertEq 4 $ next 0,
    Test "#2" $ assertEq 4 $ next 4,
    Test "#3" $ assertEq 7 $ next 5,
    Test "#4" $ assertEq 7 $ next 7,
    Test "#5" $ assertEq 44 $ next 8,
    Test "#6" $ assertEq 44 $ next 44,
    Test "#7" $ assertEq 47 $ next 45,
    Test "#8" $ assertEq 47 $ next 47,
    Test "#9" $ assertEq 74 $ next 48,
    Test "#10" $ assertEq 74 $ next 74,
    Test "#11" $ assertEq 77 $ next 75,
    Test "#12" $ assertEq 77 $ next 77,
    Test "#13" $ assertEq 444 $ next 78,
    Test "#14" $ assertEq 74744 $ next 74572,
    Test "#15" $ assertEq 77444 $ next 74872,
    Test "#16" $ assertEq 444444 $ next 79999,
    Test "#17" $ assertEq 4444 $ next 4099,
    Test "#18" $ assertEq 4744 $ next 4499,
    Test "#19" $ assertEq 44 $ next 15
  ]

testNextBig :: Test
testNextBig  =  Test "TestNextBig" $ assertEq 4444444444 $ next 777777778

testBig :: Test
testBig  =  Test "TestBig" $ assertEq 444444444 $ solve 380000000 380000000

testBigBig :: Test
testBigBig  =  Test "TestBigBig" $ assertEq 0 $ solve 1 (10^9)

testWA28 :: Test
testWA28  =  Test "Test WA #28" $ assertEq 606732 $ solve 369 852

testEqDifferentSolve :: Test
testEqDifferentSolve  =  Test "TestEqDifferentSolve" $
    assertEq [] $ [(i, j) | i <- [1..100], j <- [i..100], solve i j /= solve' i j]
  where
    solve' l r  =  sum [next i | i <- [l .. r]]

test :: IO ()
test  =  mapM_ run
  [
    testInput,
    testNext,
    testNextBig,
    testBig,
    testBigBig,
    testWA28,
    testEqDifferentSolve
  ]
-}
--------------------------------------------------------------------------------

next :: Integer -> Integer
next  =  read . next' [] . reverse . show
  where
    next' acc []  =  acc
    next' acc (c:s)
      | c <  '4'  =  next' ('4' : (map (const '4') acc)) s
      | c == '4'  =  next' ('4' : acc) s
      | c <  '7'  =  next' ('7' : (map (const '4') acc)) s
      | c == '7'  =  next' ('7' : acc) s
      | otherwise  =  nextAdd ('4' : (map (const '4') acc)) s
    nextAdd acc []  =  '4' : acc
    nextAdd acc (c:s)
      | c == '9'  =  nextAdd ('4' : acc) s
      | otherwise  =  next' acc (succ c : s)

solve :: Integer -> Integer -> Integer
solve l r  =  solve' l 0
  where
    solve' l acc
      | nextL >= r  =  acc + (r - l + 1) * nextL
      | otherwise   =  solve' (nextL + 1) (acc + (nextL - l + 1) * nextL)
      where
        nextL  =  next l

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

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

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

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

main = print =<< solve . evalState parseInput <$> BS.getContents

lucky digits = [sum subset * 3 + ones | subset <- subsequences masks]
  where
    ones = (10^digits-1) `div` 9 * 4
    masks = map (10^) [0..digits-1]

luckys = sort $ concat [lucky digits | digits <- [1..10]]

solve (l, r) = sum [ (min r b - max l (a + 1) + 1) * b
                   | (a, b) <- intervals
                   , l <= b && r > a
                   ]
  where
    intervals = zip (0:luckys) luckys

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

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

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

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

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

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

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

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

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

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

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

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

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

state = State

import Data.Char
import Debug.Trace

fours n = replicate n '4'

next n =
    if all (=='7') n then
        fours (length n + 1)
    else
        let rn = reverse n
            (a,(b:bs)) = span (=='7') rn
        in
          (reverse bs) ++ ('7':(fours $ length a))

minLucky n_ = let n = show n_
                  (nf,nb) = span (\c -> c=='4'||c=='7') n
              in
                if null nb then
                    n
                else
                    let a = (ord (head nb))-(ord '0') in
                    if a>7 then
                        (next nf)++(fours (length nb))
                    else if a>4 then
                             nf++('7':(fours (length nb - 1)))
                         else
                             nf++(fours (length nb))

solve l r = rec l 0
    where
      rec i sum | i>r = show sum
                | otherwise =
                    let minl = read (minLucky i)::Integer
                        fr_ = minl - i +1
                        fr = if (i+fr_)>r then (r-i+1) else fr_ in
                    rec (i+fr) (sum+minl*fr)

main = do lis <- getLine
          let (l:r:_) = words lis
          putStrLn $ solve (read l::Integer) (read r::Integer)

module Main (main) where

import System
import Data.Char
import Data.List
import Control.Monad
import Control.Applicative

luckyNumberList upper = sort $ tail $ makeLucky 0
              where
                makeLucky now
                  | now <= upper = [fromIntegral now] ++ makeLucky (now*10+4) ++ makeLucky (now*10+7)
                  | otherwise = []

calc :: [Integer] -> Integer -> Integer
calc [x1,x2] n = (max 0 ((min x2 n)- x1)) * x2
calc (x1:x2:xs) n = (max 0 ((min x2 n)- x1)) * x2 + calc (x2:xs) n

--fromIntegral n
sub :: Integer -> Integer
sub 0 = 0
sub n = if length list == 0 then 0
        else
          fromIntegral $ calc list n
            where list = [0] ++ luckyNumberList (n*10)

solve :: [Integer] -> Integer
solve [l,r] = sub r - sub (l - 1)

--main = interact$show.sub.read.head.words
main = interact$show.solve.map read.take 2.words

import Data.List

strToInt :: String -> Integer
strToInt = read

-- get soreted list of lucky numbers.
luckyNumbers x = sort $ f 0 x
  where
    f b x
      | b*10+7 <= x = (b*10+4):(b*10+7):[] ++ f (b*10+4) x ++ f (b*10+7) x
      | b*10+4 <= x = (b*10+4):[]
      | otherwise = []

-- solve the problem
--- x is next(l)
solve lucky l r i = let x = lucky!!i in
  if x < r then (x-l+1)*x + solve lucky (x+1) r (i+1)
  else (r-l+1)*x

main = do
  input <- getLine
  let (l:r:_) = map strToInt $ words input
  let lucky = luckyNumbers (10*r)
  putStrLn $ case findIndex (>=l) lucky of
    Just(i) -> show $ solve lucky l r i
    Nothing -> ""

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

lucky 1 = ["4","7"]
lucky n = [a:b|a<-"47",b<-lucky (n-1)]

luck::[Integer]
luck = map read $ concat [lucky i | i<-[1..]] ::[Integer]

process::Integer->Integer->Integer->[Integer]->Integer
process a b c (s:ss) | s<b = process (s+1) b (c+(s-a+1)*s) ss
	             |otherwise = c+ (b-a+1) *s
main=do
	[a,b]<-map read.words <$>getLine ::IO [Integer]
	print $ process a b 0 $ dropWhile (<a)  luck

module Main where
import Data.Char

isLucky :: Integer -> Bool
isLucky x = let s = show x
            in not $ any (flip elem ['1', '2', '3', '5', '6', '8', '9', '0']) s

lst :: [Integer]
lst = let ar = ['0', '4', '7']
          f :: String -> Bool
          f = hasOnlyLeadingZeroes
       in [read x | a<-ar, b<-ar, c<-ar, d<-ar, e<-ar, ff<-ar,g<-ar, h<-ar, i<-ar, j<-ar,let x = a:b:c:d:e:ff:g:h:i:j:[], f x]

hasOnlyLeadingZeroes :: String-> Bool
hasOnlyLeadingZeroes s = let
                            (a, b) = foldl (\(b1, b0) a ->
                                case a of
                                  '0' -> if b1 && b0 then (b1, not b0) else (b1, b0)
                                  _ -> if b1 then (b1, b0) else (not b1, b0)
                                    ) (False, True) s
                          in a && b

-- isLucky' :: Integer -> Bool
-- isLucky' x = go (x `div` 10) (x `mod` 10)
--   where
--     go :: Integer -> Integer -> Bool
--     go 0 dig = True
--     go new dig = if dig == 7 || dig == 4
--                  then go (new `div` 10) (x `mod` 10)
--                  else False

main :: IO ()
main = do
  interact $ unlines . map solve . lines

solve :: String -> String
solve st = let
             (l:r:[]) = map read . words $ st :: [Integer]
             (snn, _) = func l r
           in
             if (l == r) then show $ nextl l
             else show snn

nextl :: Integer -> Integer
nextl l = let ll = head [x | x <- lst, x >=l]
          in ll

-- func :: Integer -> Integer -> (Integer, [Integer])
-- func l r = let ll = [x | x <- lst, x >= l && x <= r]
--                h = head ll
--                lsl = nextl r
--                (_, llln) = foldl (\(b, l) a -> (a, (replicate (fromInteger $ a - b) a) : l)) (h, []) (tail ll)
--                lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - last ll) lsl
--            in (sum $ lln,lln)

func :: Integer -> Integer -> (Integer, [Integer])
func l r = let ll = [x | x <- lst, x >= l && x <= r]
               h = head ll
               lsl = nextl r
               lsll = last ll
               (_, llln, sn) = foldl (\(b, l, s) a -> (a, ((replicate (fromInteger $ a - b) a) : l), (s + a * (a-b)))) (h,[],0) (tail ll)
               lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - lsll) lsl
               snn = sn + h * (h - l + 1) + lsl * (r - lsll)
           in
           if length ll == 0
           then ((r - l + 1) * nextl l, [])
           else (snn,lln)

-- Codeforces 122C

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = 2 + Data.List.sum (map (2 ^) [1..9])

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 444444444 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)
solve _ = undefined

lucky :: [Integer]
lucky = map read $ concat $ tail $ take 11 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)

lucky :: [Integer]
lucky = map read $ (id =<<) $ tail $ take 11 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

import Control.Monad (replicateM)
luckys :: [Integer]
luckys = map read $ concatMap (flip replicateM ['4', '7']) [1..10]

solve' :: Integer -> Integer -> [Integer] -> Integer
solve' _ _ [] = 0
solve' x y (k:ks)
  | y < x = 0
  | x > k = solve' x y ks
  | otherwise = (min y k - x + 1) * k + solve' (k + 1) y ks
parse :: String -> Integer
parse s = case map read (words s) of [x, y] -> solve' x y luckys

main = putStrLn . show . parse =<< getLine

lucky = let ys@(y : _) = ["4","7"] : map (\x -> map ("4"++) x ++ map ("7"++) x )ys in map (\x -> map (\y -> (read y) :: Integer) x) ys

count x = length $ show x

solve l r = doIt l r enum 0
	where
		s = count l
		enum = concat $ (drop (s-1) lucky )
		doIt l r (x : xs) acc		|	x < l		=	doIt l r xs acc
						|	x > r		=	acc + (r - l + 1) * x
						|	otherwise	=	($!) (doIt (x+1) r xs) (acc + (x - l + 1) * x)

main :: IO ()

main = do
		ln <- getLine
		let (l : r : _) = map (\x -> (read x) :: Integer) $ words ln
		print $ solve l r
		return ()

{-
import HUnit

testInput :: Test
testInput  =  TestList "TestInput"
  [
    Test "#1" $ assertEq 33 $ solve 2 7,
    Test "#2" $ assertEq 7 $ solve 7 7
  ]

testNext :: Test
testNext  =  TestList "TestNext"
  [
    Test "#1" $ assertEq 4 $ next 0,
    Test "#2" $ assertEq 4 $ next 4,
    Test "#3" $ assertEq 7 $ next 5,
    Test "#4" $ assertEq 7 $ next 7,
    Test "#5" $ assertEq 44 $ next 8,
    Test "#6" $ assertEq 44 $ next 44,
    Test "#7" $ assertEq 47 $ next 45,
    Test "#8" $ assertEq 47 $ next 47,
    Test "#9" $ assertEq 74 $ next 48,
    Test "#10" $ assertEq 74 $ next 74,
    Test "#11" $ assertEq 77 $ next 75,
    Test "#12" $ assertEq 77 $ next 77,
    Test "#13" $ assertEq 444 $ next 78,
    Test "#14" $ assertEq 74744 $ next 74572,
    Test "#15" $ assertEq 77444 $ next 74872,
    Test "#16" $ assertEq 444444 $ next 79999,
    Test "#17" $ assertEq 4444 $ next 4099,
    Test "#18" $ assertEq 4744 $ next 4499
  ]

testNextBig :: Test
testNextBig  =  Test "TestNextBig" $ assertEq 4444444444 $ next 777777778

testBig :: Test
testBig  =  Test "TestBig" $ assertEq 444444444 $ solve 380000000 380000000

testBigBig :: Test
testBigBig  =  Test "TestBigBig" $ assertEq 0 $ solve 1 (10^9)

test :: IO ()
test  =  mapM_ run
  [
    testInput,
    testNext,
    testNextBig,
    testBig,
    testBigBig
  ]
-}
--------------------------------------------------------------------------------

next :: Integer -> Integer
next  =  read . next' [] . reverse . show
  where
    next' acc []  =  acc
    next' acc (c:s)
      | c <= '4'  =  next' ('4' : acc) s
      | c <  '7'  =  next' ('7' : (map (const '4') acc)) s
      | c == '7'  =  next' ('7' : acc) s
      | otherwise  =  nextAdd ('4' : (map (const '4') acc)) s
    nextAdd acc []  =  '4' : acc
    nextAdd acc (c:s)
      | c == '9'  =  nextAdd ('4' : acc) s
      | otherwise  =  next' acc (succ c : s)

solve :: Integer -> Integer -> Integer
solve l r  =  solve' l 0
  where
    solve' l acc
      | nextL >= r  =  acc + (r - l + 1) * nextL
      | otherwise   =  solve' (nextL + 1) (acc + (nextL - l + 1) * nextL)
      where
        nextL  =  next l

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

-- Codeforces 121A

{-# OPTIONS_GHC -O2 #-}
{-# OPTIONS_GHC -optc-O2 #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST.Safe
import Data.Array.IArray
import Data.Array.MArray.Safe
import Data.Array.ST.Safe
import Data.Foldable
import Data.Int
import Data.List
import Data.STRef

foreach :: Monad m => [a] -> b -> (b -> a -> m b) -> m b
foreach xs v f = foldM f v xs

-- range: [, ], left inclusive and right inclusive
qsort :: (MArray arr e m, Ord e) => arr Int e -> Int -> Int -> m ()
qsort = qsortImpl
  where
    qsortImpl arr l r = when (r > l) $ do
        let mid = l + (r - l) `div` 2
        nmid <- partition arr l r mid
        qsortImpl arr l (nmid - 1)
        qsortImpl arr (nmid + 1) r
    swap arr ia ib = do
        a <- readArray arr ia
        b <- readArray arr ib
        writeArray arr ia b
        writeArray arr ib a
    partition arr l r mid = do
        pivot <- readArray arr mid
        swap arr mid r
        slot <- foreach [l..r-1] l (\slot i -> do
            val <- readArray arr i
            if val < pivot
               then swap arr i slot >> return (slot+1)
               else return slot)
        swap arr slot r >> return slot

main :: IO ()
main = do

    let nlen = Data.List.sum $ map (2 ^) [1..9]

    let arr = runSTUArray $ do
          arr <- newArray_ (0, nlen) :: ST s (STUArray s Int Int64)
          let go k top arr | k > 100000000 = return ()
                           | otherwise = do
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 4)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 4) top arr
                                z <- readSTRef top
                                writeArray arr z (k * 10 + 7)
                                modifySTRef' top (+ 1)
                                go (k * 10 + 7) top arr

          writeArray arr 0 0
          top <- newSTRef 1
          go 0 top arr
          z <- readSTRef top
          qsort arr 1 nlen
          return arr

    let sumAll x | x == 0 = 0
                 | otherwise = go 1
                 where go i | i > nlen = 0
                            | x >= a = a * (a - b) + go (i + 1)
                            | otherwise = a * (x - b)
                            where a = arr ! i
                                  b = arr ! (i - 1)

    [a, b] <- (map read . words) <$> getLine :: IO [Int64]
    print $ sumAll b - sumAll (a - 1)

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

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

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

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

solve (n, s) = BS.unlines $ replicate n s

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

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

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

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

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

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

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

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

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

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

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

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

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

state = State

import Data.Char
import Debug.Trace

fours n = replicate n '4'

next n =
    if all (=='7') n then
        fours (length n + 1)
    else
        let rn = reverse n
            (a,(b:bs)) = span (=='7') rn
        in
          (reverse bs) ++ ('7':a)

minLucky n_ = let n = show n_
                  (nf,nb) = span (\c -> c=='4'||c=='7') n
              in
                if null nb then
                    n
                else
                    let a = (ord (head nb))-(ord '0') in
                    if a>7 then
                        (next nf)++(fours (length nb))
                    else if a>4 then
                             nf++('7':(fours (length nb - 1)))
                         else
                             nf++(fours (length nb))

solve l r = rec l 0
    where
      rec i sum | i>r = show sum
                | otherwise =
                    let minl = read (minLucky i)::Integer
                        fr_ = minl - i +1
                        fr = if (i+fr_)>r then (r-i+1) else fr_ in
                    rec (i+fr) (sum+minl*fr)

main = do lis <- getLine
          let (l:r:_) = words lis
          putStrLn $ solve (read l::Integer) (read r::Integer)

import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as C
import Data.Maybe
import Data.Array
import qualified Data.IntMap as I
import qualified Data.Map as M

lucky 1 = ["4","7"]
lucky n = [a:b|a<-"47",b<-lucky (n-1)]

luck::[Int]
luck = map read $ concat [lucky i | i<-[1..]] ::[Int]

process a b c (s:ss) | s<b = process (s+1) b (c+(s-a+1)*s) ss
	             |otherwise = c+ (b-a+1) *s
main=do
	[a,b]<-map read.words <$>getLine ::IO [Int]
	print $ process a b 0 $ dropWhile (<a)  luck

module Main where
import Data.Char

isLucky :: Integer -> Bool
isLucky x = let s = show x
            in not $ any (flip elem ['1', '2', '3', '5', '6', '8', '9', '0']) s

lst :: [Integer]
lst = let ar = ['0', '4', '7']
          f :: String -> Bool
          f = hasOnlyLeadingZeroes
       in [read x | a<-ar, b<-ar, c<-ar, d<-ar, e<-ar, ff<-ar,g<-ar, h<-ar, i<-ar, j<-ar,let x = a:b:c:d:e:ff:g:h:i:j:[], f x]

hasOnlyLeadingZeroes :: String-> Bool
hasOnlyLeadingZeroes s = let
                            (a, b) = foldl (\(b1, b0) a ->
                                case a of
                                  '0' -> if b1 && b0 then (b1, not b0) else (b1, b0)
                                  _ -> if b1 then (b1, b0) else (not b1, b0)
                                    ) (False, True) s
                          in a && b

-- isLucky' :: Integer -> Bool
-- isLucky' x = go (x `div` 10) (x `mod` 10)
--   where
--     go :: Integer -> Integer -> Bool
--     go 0 dig = True
--     go new dig = if dig == 7 || dig == 4
--                  then go (new `div` 10) (x `mod` 10)
--                  else False

main :: IO ()
main = do
  interact $ unlines . map solve . lines

solve :: String -> String
solve st = let
             (l:r:[]) = map read . words $ st :: [Integer]
             (snn, _) = func l r
           in
             if (l == r) then show $ nextl l
             else show snn

nextl :: Integer -> Integer
nextl l = let ll = head [x | x <- lst, x >=l]
          in ll

-- func :: Integer -> Integer -> (Integer, [Integer])
-- func l r = let ll = [x | x <- lst, x >= l && x <= r]
--                h = head ll
--                lsl = nextl r
--                (_, llln) = foldl (\(b, l) a -> (a, (replicate (fromInteger $ a - b) a) : l)) (h, []) (tail ll)
--                lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - last ll) lsl
--            in (sum $ lln,lln)

func :: Integer -> Integer -> (Integer, [Integer])
func l r = let ll = [x | x <- lst, x >= l && x <= r]
               h = head ll
               lsl = nextl r
               lsll = last ll
               (_, llln, sn) = foldl (\(b, l, s) a -> (a, ((replicate (fromInteger $ a - b) a) : l), (s + a * (a-b)))) (h,[],0) (tail ll)
               lln = replicate (fromInteger $ h - l +1) h ++ concat llln ++ replicate (fromInteger $ r - lsll) lsl
               snn = sn + h * (h - l + 1) + lsl * (r - lsll)
           in
           if length ll == 0
           then ((r - l) * nextl l, [])
           else (snn,lln)

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Integer] -> Integer
solve [l, r] = sumLucky r - sumLucky (l - 1)

lucky :: [Integer]
lucky = map read $ (id =<<) $ tail $ take 9 $ iterate (\xs -> map ('4':) xs ++ map ('7':) xs) [""]

sumLucky :: Integer -> Integer
sumLucky n = sum $ map snd $ scanl f (0, 0) lucky
  where f (x, _) z | z <= n = (z, (z - x) * z)
                   | x >= 0 = (-1, (n - x) * z)
                   | otherwise = (-1, 0)

import Control.Monad (replicateM)
luckys :: [Integer]
luckys = map read $ concatMap (flip replicateM ['4', '7']) [1..9]

solve' :: Integer -> Integer -> [Integer] -> Integer
solve' _ _ [] = 0
solve' x y (k:ks)
  | y < x = 0
  | x > k = solve' x y ks
  | otherwise = (min y k - x + 1) * k + solve' (k + 1) y ks
parse :: String -> Integer
parse s = case map read (words s) of [x, y] -> solve' x y luckys

main = putStrLn . show . parse =<< getLine

lucky = let ys@(y : _) = ["4","7"] : map (\x -> map ("4"++) x ++ map ("7"++) x )ys in map (\x -> map (\y -> (read y) :: Int) x) ys

count x = length $ show x

solve l r = doIt l r enum 0
	where
		s = count l
		enum = concat $ (drop (s-1) lucky )
		doIt l r (x : xs) acc		|	x < l		=	doIt l r xs acc
						|	x > r		=	acc + (r - l + 1) * x
						|	otherwise	=	($!) (doIt (x+1) r xs) (acc + (x - l + 1) * x)

main :: IO ()

main = do
		ln <- getLine
		let (l : r : _) = map (\x -> (read x) :: Int) $ words ln
		print $ solve l r
		return ()


main = do
    n <- readLn
    print $ solve n

solve n | n == 1 = 1
        | n == 2 = 2
        | odd n = n * (n-1) * (n-2)
        | otherwise = max ((n-1)*(n-2)*(n-3)) $ maximum $ do
            let a = n
            let b = n-1
            c <- [1..n-2]
            return $ a `lcm` b `lcm` c

prime :: (Integral a, Ord a) => a -> a -> a -> Bool
prime 1 cur comp = False
prime n cur comp
	|n `mod` 2 == 0 = False
	|cur >= comp = True
	|otherwise = if n `mod` cur == 0 then False else prime n (cur+2) comp

get :: Int -> Float -> [Integer]
get 3 cur = []
get x cur = if prime (truncate $ cur) 3 (truncate . sqrt $ cur) == True then (truncate cur):(get (x+1) (cur-1)) else get x (cur-1)

mx :: (Ord a) => [a] -> a
mx xs = foldl (\acc x -> max acc x) (head xs) xs

find :: [Integer] -> Integer
find xs = mx $ [x*(kk y z) `div` (gcd x (kk y z)) | x <- [last $ xs..(head $ xs)], y <- [x+1 .. (head xs)], z <- [y+1 .. head xs]]
					where kk y z = y*z `div` (gcd y z)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ find $ ((truncate n):(get 0 n))

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

solve :: Integer -> Integer
solve n = maximum [ a `lcm` b `lcm` c | a <- [n,n-1..p],b <- [a,a-1..p],c <- [b,b-1..p] ]
  where p = last (take 3 $ take 3 (filter isPrime [n,n-1..1]) ++ [1])

primes :: Integral a => [a]
primes = 2 : filter isPrime [3,5..]

isPrime :: Integral a => a -> Bool
isPrime n = n > 1 && foldr (\p r -> p * p > n || (n `mod` p /= 0 && r)) True primes

main=readLn>>=print.solve
solve :: Integer -> Integer
solve x = maximum [i`lcm`j`lcm`k|i<-[z..x],j<-[z..x],k<-[z..x]]
   where
     y = head $ filter ((<2).gcd x) [x,x-1..1]
     z = head $ filter ((<2).gcd (x*y)) [y,y-1..1]

main = do
  n <- fmap read getLine
  print $ maximum $ [foldl1 lcm [i, j, k] |
    m <- [max 1 (n - 10)],
    i <- [m .. n],
    j <- [m .. n],
    k <- [m .. n]]

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    let ans = max (max (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n)))) (div ((pr (pr k))*(pr k)*k) (gcd k (pr (pr k))))) (div ((pr (pr k))*(pr n)*n) (gcd n (pr (pr k)))) where
        k = pr n
    print ans

import Data.List
import Monad
import Prelude hiding (Int)

type Int  =  Integer

isPrime :: Int -> Bool
isPrime n  =  all (\x -> mod n x /= 0) $ takeWhile (\x -> x * x <= n) primes

primes :: [Int]
primes  =  2 : [n | n <- [3,5..], isPrime n]

maxPrime :: Int -> Int
maxPrime n  =  last $ takeWhile (< n) primes

lcmS :: [Int] -> Int
lcmS  =  foldl' lcm 1

solve :: Int -> Int
solve n  =  maximum $
    [lcmS [a, b, c] | a <- [m..n], b <- [a..n], c <- [b..n]]
  where
    m  =  truncate ((fromIntegral (lcmS [n,n-1,n-2])) ** (1/3 :: Double)) - 1

solve2 :: Int -> Int
solve2 1  =  1
solve2 2  =  2
solve2 4  =  12
solve2 n
  | odd n   =  lcmS [n, n-1, n-2]
  | n `mod` 3 == 0  =  lcmS [n-1, n-2, n-3]
  | otherwise  =  lcmS [n, n-1, n-3]

main :: IO ()
main  =  do
    n  <-  readLn
    print $ solve2 n

solve l1
    | n == 1 = 1
    | otherwise = maximum [m*(m-1)*head [k | k <- reverse [1..m], gcd k (m*(m-1)) == 1] | m <- [(max 1 (n-7))..n]]
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))

main = readLn >>= print . maxlcm3
       where maxlcm3 n
       		  | n < 3 		   = n
			  | mod n 6 == 0   = lcm3l [n-1,n-2, n-3]
			  | even n         = lcm3l [n,  n-1, n-3]
			  | otherwise      = lcm3l [n,  n-1, n-2]
			  where lcm3l [a,b,c] = a `lcm` b `lcm` c

-- lcm :: [Int] -> Int
-- lcm xs = lcm' [(d, divisor_degree d x) | d <- divisors x, x <- xs]

-- divisor_degree :: Int -> Int -> Int
-- divisor_degree d n
--   | mod n d == 0 = 0
--   | otherwise = 1 + (divisor_degree d (div n d))

-- divisors :: Int -> [Int]
-- divisors n = [d| d <- [1..(floor $ sqrt n)], (mod n d) == 0]

-- prime_divisors :: Int -> [Int]
-- prime_divisors n = [p|p <- divisors n, divisors p == [1,p]]

-- prime_divisors :: Int -> [(Int, Int)]
-- num2primedivisordegs n = [(p, divisor_degree d n) | d <- prime_divisors n]

-- divdegpairs2lcm :: [(Int, Int)] -> [(Int, Int)] -> Int
-- divdegpairs2lcm [] zs = zs
-- divdegpairs2lcm zs [] = zs
-- divdegpairs2lcm x:xs y:ys = if

-- lcm' :: [(Int, Int)] ->
-- lcm' ((div,deg):xs) = [ (div, max (deg)) | x <- xs]
-- maxdegree (a, dega) (a, degb) = (a, max dega degb)

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

import Data.Word

main = do
    x <- getLine
    putStrLn $ show $ f (read x :: Word64)

f :: (Integral a) => a -> a
f x
    | x<3 = x
    | (mod x 2 == 0) && (mod x 3 == 0) = (x-1)*(x-2)*(x-3)
    | mod x 2 == 0 = x*(x-1)*(x-3)
    | otherwise = x*(x-1)*(x-2)

f:: Integer -> Integer
f n
	|(n `mod` 2 == 0) && (n `mod` 3 == 0) = (n-1)*(n-2)*(n-3)
	|n `mod` 2 == 0 = n*(n-1)*(n-3)
	|otherwise = n*(n-1)*(n-2)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    print ((pr (pr n))*(pr n)*n)

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    let ans = max (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n)))) (div ((pr (pr k))*(pr k)*k) (gcd k (pr (pr k)))) where
        k = pr n
    print ans

pr :: Integer -> Integer
pr 1 = 1
pr n = n-1
main = do
    sn <- getLine
    let n = read sn
    print (div ((pr (pr n))*(pr n)*n) (gcd n (pr (pr n))))

main = readLn >>= print . maxlcm3
       where maxlcm3 n
			  | mod n 6 == 0   = lcm3l [n-1,n-2, n-3]
			  | even n         = lcm3l [n,  n-1, n-3]
			  | otherwise      = lcm3l [n,  n-1, n-2]
			  where lcm3l [a,b,c] = a `lcm` b `lcm` c

-- lcm :: [Int] -> Int
-- lcm xs = lcm' [(d, divisor_degree d x) | d <- divisors x, x <- xs]

-- divisor_degree :: Int -> Int -> Int
-- divisor_degree d n
--   | mod n d == 0 = 0
--   | otherwise = 1 + (divisor_degree d (div n d))

-- divisors :: Int -> [Int]
-- divisors n = [d| d <- [1..(floor $ sqrt n)], (mod n d) == 0]

-- prime_divisors :: Int -> [Int]
-- prime_divisors n = [p|p <- divisors n, divisors p == [1,p]]

-- prime_divisors :: Int -> [(Int, Int)]
-- num2primedivisordegs n = [(p, divisor_degree d n) | d <- prime_divisors n]

-- divdegpairs2lcm :: [(Int, Int)] -> [(Int, Int)] -> Int
-- divdegpairs2lcm [] zs = zs
-- divdegpairs2lcm zs [] = zs
-- divdegpairs2lcm x:xs y:ys = if

-- lcm' :: [(Int, Int)] ->
-- lcm' ((div,deg):xs) = [ (div, max (deg)) | x <- xs]
-- maxdegree (a, dega) (a, degb) = (a, max dega degb)

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

main = do
    x <- getLine
    putStrLn $ show $ f (read x :: Integer)

f :: (Integral a) => a -> a
f x
    | x<3 = x
    | mod x 2 == 0 = x*(x-1)*(x-3)
    | otherwise = x*(x-1)*(x-2)

f:: Integer -> Integer
f n
	|n `mod` 2 == 1 = n*(n-1)*(n-2)
	|n `mod` 3 /= 0 = n*(n-1)*(n-3)
	|otherwise = n*(n-1)*(n-2) `div ` 2

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

prime :: (Integral a, Ord a) => a -> a -> a -> Bool
prime 1 cur comp = False
prime n cur comp
	|cur >= comp = True
	|n `mod` 2 == 0 = False
	|otherwise = if n `mod` cur == 0 then False else prime n (cur+2) comp

get :: Int -> Float -> [Integer]
get 3 cur = []
get x cur = if prime (truncate $ cur) 3 (truncate . sqrt $ cur) == True then (truncate cur):(get (x+1) (cur-1)) else get x (cur-1)

mx :: (Ord a) => [a] -> a
mx xs = foldl (\acc x -> max acc x) (head xs) xs

find :: [Integer] -> Integer
find xs = mx $ [x*(kk y z) `div` (gcd x (kk y z)) | x <- [last $ xs..(head $ xs)], y <- [x+1 .. (head xs)], z <- [y+1 .. head xs]]
					where kk y z = y*z `div` (gcd y z)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ find $ ((truncate n):(get 0 n))

f:: Integer -> Integer
f n
	|n `mod` 2 == 1 = n*(n-1)*(n-2)
	|otherwise = n*(n-1)*(n-3)

main = do
	n <- readLn
	if n==1 then print $ 1 else
		if n==2 then print $ 2 else
			if n==3 then print $ 6 else
				if n==4 then print $ 12 else
				 print $ (f n)

main=readLn>>=print.solve
solve :: Integer -> Integer
solve x = x*y*z
   where
     y = head $ filter ((<2).gcd x) [x,x-1..1]
     z = head $ filter ((<2).gcd (x*y)) [y,y-1..1]

solve l1
    | n == 1 = 1
    | otherwise = n * (n - 1) * head [n-k | k <- [1..], gcd n (n-k) == 1, gcd (n-1) (n-k) == 1]
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))

solve l1
    | n == 1 = 1
    | otherwise = n*(n-1)*(maximum [k `div` (gcd k (n*(n-1))) | k <- [1..n]])
    where n = read l1 :: Integer
main = do
    l1 <- getLine
    putStrLn (show (solve l1))


{-# OPTIONS_GHC -O2 #-}

import Data.Char (ord)

parseInt = toInteger . foldl (\acc c -> acc * 10 + ord c - 48) 0

parseInts = fmap parseInt . words

power :: Integer -> Integer
power n
  | n == 0 = 1
  | even n = (ret * ret) `mod` mm
  | otherwise = (ret * ret) `mod` mm * 2 `mod` mm
  where
    ret = power (n `div` 2)
    mm = 998244353

main = do
  [n, m] <- fmap parseInts getLine
  print (power (n + m))

magic :: Integer
magic = 998244353

power :: Integer -> Integer -> Integer -> Integer
power base expo modulo
    | expo == 0 = 1
    | expo == 1 = base `mod` modulo
    | odd expo  = base * (power base (expo - 1) modulo) `mod` modulo
    | otherwise = let root = power base (expo `div` 2) modulo
                  in root * root `mod` modulo

main = do
    nums <- (map (\tok -> read tok :: Integer) . words) <$> getLine
    putStrLn . show $ power 2 (sum nums) magic

magic :: Int
magic = 998244353

power :: Int -> Int -> Int -> Int
power base expo modulo
    | expo == 0 = 1
    | expo == 1 = base `mod` modulo
    | odd expo  = base * (power base (expo - 1) modulo) `mod` modulo
    | otherwise = let root = power base (expo `div` 2) modulo
                  in root * root `mod` modulo

main = do
    nums <- (map (\tok -> read tok :: Int) . words) <$> getLine
    putStrLn . show $ power 2 (sum nums) magic


import Control.Monad
import Data.List

getStrList :: IO [String]
getStrList = words <$> getLine

getIntList :: IO [Int]
getIntList = map read <$> getStrList

main :: IO ()
main = putStrLn "NO"

main = do
    putStrLn "no"
    return ()

import Control.Monad

main = do
    putStrLn "No"

 import Data.Char

 main = getLine >>= print . (`mod` 2) . digitToInt . last


import qualified Data.Map.Strict as M
import qualified Data.Set as S
import Data.List (foldl')
import Data.Maybe (fromMaybe)

main = do
  bulbs <- getLine
  putStrLn $
    let onlyBulbs = filter (flip S.member (S.fromList "RGBY!")) bulbs
        positioned = zip [0..] onlyBulbs
        onlyValid = filter ((/=) '!' . snd) positioned
        zero = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 0) onlyValid
        one = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 1) onlyValid
        two = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 2) onlyValid
        three = snd . head $ filter (\(pos, char) -> (pos `mod` 4) == 3) onlyValid
        findColor pos =
          case pos `mod` 4 of
            0 -> zero
            1 -> one
            2 -> two
            3 -> three
        counted =
          foldl
            (\map (pos, char) ->
                if char == '!' then
                  M.insertWith (+) (findColor pos) 1 map
                else map)
            M.empty
            positioned
    in
      unwords . map show . map (fromMaybe 0 . flip M.lookup counted) $ ['R', 'B', 'Y', 'G']

import Data.List
import Data.Ord
import Data.Char
fi = fromIntegral

decim [] = []
decim (x:xs) =x:decim (drop 3 xs)

calc seq = (color,num) where
	color = head $ filter (/='!') seq
	num = length $ filter (=='!') seq

solve seq = unwords $ map show [r,b,y,g] where
	all= map (\n->calc.decim.drop n $ seq) [0..3]
	r=snd.head.filter (\(c,n)->c=='R') $ all
	b=snd.head.filter (\(c,n)->c=='B') $ all
	y=snd.head.filter (\(c,n)->c=='Y') $ all
	g=snd.head.filter (\(c,n)->c=='G') $ all

main=do
	d<-getLine
	putStrLn $ solve d

{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Maybe
import Data.Char
import qualified Data.ByteString.Char8 as B
import Text.Printf

readInt = ( readLn :: IO Int )
readInts = map ( read :: String -> Int ) . words <$> getLine

getList = map ( fst . fromJust . B.readInt ) . B.words <$> B.getLine

which a b f = if f then a else b
mp [ a, b ] = ( a, b )

main = do
	s <- getLine
	let
		t = cycle $ head $ filter ( and . zipWith match s . cycle ) $ permutations chars
		r = broke s t
		[ a, b, c, d ] = map ( \c -> length $ filter ( == c ) r ) chars
	printf "%d %d %d %d\n" a b c d

chars = "RBYG"

match c t
	| c == '!' = True
	| otherwise = c == t

broke [] _ = ""
broke ( a : s ) ( b : t )
	| a == '!' = b : broke s t
	| otherwise = broke s t


solve [n, x1, y1, x2, y2]
    | (dir x1 y1) == (dir x2 y2) = abs ((dist x1 y1) - (dist x2 y2))
    | otherwise                  = min summa (abs $ 4 * n - summa)
    where
      summa = (dist x1 y1) + (dist x2 y2)
      dist a b = a + b
      dir a b = a > b

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

main = interact (show . solve . parse)

module Main where

import Control.Monad

main = do
    [n, x1, y1, x2, y2] <- ( map read . words ) `liftM` getLine
    let p1 = linePos x1 y1
        p2 = linePos x2 y2
        linePos x y = if y == 0
            then x
            else if y == n
                then 3 * n - x
                else if x == n
                    then n + y
                    else 4 * n - y
        d = min ( abs $ p1 - p2 ) ( 4 * n - ( abs $ p1 - p2 ) )
    putStrLn $ show d

dist :: Int -> (Int, Int) -> Int
dist _ (0, y) = y
dist n (x, 0) = 4 * n - x
dist n (x, y)
	| y == n = n + x
	| otherwise = 3 * n - y

min_dist :: Int -> Int -> Int
min_dist n d = min d (4 * n - d)

main = do
	n:x1:y1:x2:y2:_ <- getLine >>= return . map read . words :: IO [Int]
	putStrLn . show $ min_dist n $ abs $ (dist n (x1, y1)) - (dist n (x2, y2))

main = interact $ show . solve . (map (read :: String -> Int)) . words
solve [n,x1,y1,x2,y2] = min (w2 - w1) (4 * n + w1 - w2)
   where
     z1 = f x1 y1
     z2 = f x2 y2
     w1 = min z1 z2
     w2 = max z1 z2
     f x y
       | x == 0    = y
       | y == n    = n + x
       | x == n    = 3 * n - y
       | otherwise = 4 * n - x


import System.IO
import qualified Data.List as L

read_ints :: String -> [Int]
read_ints x = map read (words x) :: [Int]

main = do
	ln <- getLine
	let [n,m] = read_ints ln
	putStr $ show $ if n == 1 then 1 else ( if m - 1 < n - m then m+1 else m-1 )

probability::Int->Int->Int->Double
probability n m a
  | m > a = fromIntegral((a + m) `div` 2) / fromIntegral(n)
  | m < a = 1.0 - fromIntegral((a + m ) `div` 2) / fromIntegral(n)
  | otherwise = 0.0

small_candidates n m = if m - 1 >= 1 then [(m-1,probability n m (m-1))] else []
large_candidates n m = if m + 1 <= n then [(m+1,probability n m (m+1))] else []
candidates n m = small_candidates n m ++ large_candidates n m ++ [(m,0)]

getMax [] = (0,-1);
getMax (x:xs)
  | snd x >= snd candidate = x
  | otherwise = candidate
  where
    candidate = getMax xs

ans::Int->Int->Int
ans n m = fst(getMax(candidates n m))

main = do
  input <- getLine
  let [n,m] = [read x::Int | x <- words input]
  print(ans n m)

-- Codeforces Round #316 (Div. 2) - Problem B: Simple Game (https://codeforces.com/problemset/problem/570/B)

-- Corner case: só há uma escolha possível
solve 1 _ = 1
-- A resposta deve ser um dentre os dois vizinhos à esquerda ou a direita de m,
-- o mais distante dos extremos dentre eles
solve n m | (n - m) > (m - 1) = m + 1
          | otherwise         = m - 1

main = do
    line <- getLine
    let [n, m] = map read $ words line :: [Int]
    print $ solve n m

main :: IO()
main = do
    [n,m] <- fmap (map (\x -> read x::Int) . words) getLine
    if n==1 then (print 1) else if m > (n `quot` 2) then print (m-1) else print (m+1)

import Control.Applicative
import Control.Arrow
import Control.Monad
import Data.List
import Data.Function

readInts :: IO [Int]
readInts = map read . words <$> getLine

fdiv = (/) `on` fromIntegral

main = do
    [n, m] <- readInts
    print $ if n `fdiv` 2 < fromIntegral m then max 1 (m - 1)
                                           else min n (m + 1)

-- Codeforces 570B

main :: IO ()
main = do
    [n, m] <- fmap (map read . words) getLine :: IO [Integer]
    print $ if n == 1 then 1 else if m <= (n `div` 2) then m+1 else m-1

main = interact $ show . f . map read . words
f (1:m:k) = 1
f (n:1:k) = 2
f (n:m:k) = if n==m then n-1 else (if n-m-1>m-1-1 then m+1 else m-1)

import Control.Applicative
import Control.Monad

-----

--be carefull with the type inference

-----

---------
-- get space separated something

getListSp :: Read a => IO [a]
getListSp = (map read . words) <$> getLine

---------

---------
-- output [a] in space separated form

putListSp :: Show a => [a]  -> IO()
putListSp ls = putStrLn $ show $ unwords $ map show ls

---------

--------
-- read an integer N, get N lines, make a list consist of them

getListLn :: Read a => IO [a]
getListLn = do n <- readLn
               s <- replicateM n getLine
	       return (map read s)
--------

main = do [n, m] <- getListSp
          print $ solve n m

solve:: Int -> Int -> Int
solve 1 _ = 1
solve n m | (n-m) <= (m-1) = m-1
          | otherwise      = m+1

main :: IO ()
main = getLine >>= print . solve . toTuple . map read . words

solve :: (Int, Int) -> Int
solve (n, m) | m * 2 <= n = m + 1
             | n == 1 = 1
             | otherwise = m - 1

toTuple :: [a] -> (a, a)
toTuple xs = (head xs, xs !! 1)

main = interact (show . solve . map read . words)
solve [1,1] = 1
solve [n,m] | m <= n`div` 2 = m+1
            | otherwise = m-1

main :: IO()
main = do
    [n,m] <- fmap (map (\x -> read x::Int) . words) getLine
    if m > (n `quot` 2)
        then print (m-1)
        else print (m+1)

-- Codeforces 570B

main :: IO ()
main = do
    [n, m] <- fmap (map read . words) getLine :: IO [Integer]
    print $ if n == 1 then 1 else if m >= ((n+1) `div` 2) then m-1 else m+1

-- Codeforces 570B

main :: IO ()
main = do
    [n, m] <- fmap (map read . words) getLine :: IO [Int]
    print $ if n == 1 then 1 else if m <= (n+1) `div` 2 then m+1 else m-1

-- Codeforces 570B

main :: IO ()
main = do
    [n, m] <- fmap (map read . words) getLine :: IO [Int]
    print $ if m <= (n+1) `div` 2 then m+1 else m-1

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Int] -> Int
solve [n, m] | m * 2 <= n = m + 1
             | otherwise = m - 1
solve _ = undefined

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Int] -> Int
solve [n, m] | m * 2 < n = (n + m) `div` 2
             | otherwise = (m + 1) `div` 2
solve _ = undefined

main :: IO ()
main = getLine >>= print . solve . map read . words

solve :: [Int] -> Int
solve [n, m] | m * 2 < n = m + 1
             | otherwise = m - 1
solve _ = undefined

main = interact (show . solve . map read . words)
solve [n,m] | m <= n`div` 2 = m+1
            | otherwise = m-1

main = interact (show . solve . map read . words)
solve [n,m] | m < n`div` 2 = m+1
            | otherwise = m-1

-- Codeforces Round #316 (Div. 2) - Problem B: Simple Game (https://codeforces.com/problemset/problem/570/B)

-- Corner case: só há uma escolha possível
solve 1 _ = 1
-- A resposta deve ser um dentre os dois vizinhos à esquerda ou a direita de m,
-- o mais distante dos extremos dentre eles
solve n m | (n - m - 1) > (m - 1) = m + 1
          | otherwise             = m - 1

main = do
    line <- getLine
    let [n, m] = map read $ words line :: [Int]
    print $ solve n m


shortestRoute :: Int -> Int -> Int -> Int
shortestRoute d1 d2 d3 =
  minimum [(2*d1 + 2*d2), (d1 + d2 + d3), 2*d1+2*d3, 2*d2+2*d3]

parseSpace :: String -> [Int]
parseSpace ws = map read (words ws) :: [Int]

main :: IO ()
main = do
  [d1, d2, d3] <- fmap parseSpace getLine
  print $ shortestRoute d1 d2 d3

module Main where

main :: IO ()
main = do
	inp <- getLine
	let [d1, d2, d3] = readInts inp
	print $ optimize d1 d2 d3

readInts :: String -> [Int]
readInts = map read . words

optimize :: Int -> Int -> Int -> Int
optimize d1 d2 d3 = minimum [2*(d1+d2), 2*(d1+d3), 2*(d2+d3), d1+d2+d3]

import Data.List

main :: IO()
main = print . solve . map read . words =<< getLine

solve :: [Int] -> Int
solve [a, b, c] = minimum [a + a + b + b, a + c + b, a + c + c + a, b + c + c + b]

max' [x] = x
max' (x:xs) = max x (max' xs)

main = do
  line <- getLine
  let [d1, d2, d3] = map read $ words line :: [Int]
  let d4 = min d1 (d2+d3)
  let d5 = min d2 (d1+d3)
  let d6 = min d3 (d1+d2)
  let ans = d4 + d5 + d6
  print ans

solve :: [Integer] -> Integer
solve [d1, d2, d3] = minimum [(d1 + d2 + d3), (2 * (d1 + d2)), (2 * (d1 + d3)), (2 * (d2 + d3))]

main = getContents >>= putStrLn . show . solve . map read . words

main = do
  [a, b, c] <- fmap (map read . words) getLine

  print $ minimum [2*a+2*b, a+b+c, 2*a+2*c, 2*b+2*c]

--fast read
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Control.Applicative
readInts a = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.words a
readLists = readInts $ B.getLine
readLLists = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.lines $ B.getContents
readMatrix = fmap readInts $ fmap B.lines $ B.getContents
main = do
  [d0,d1,d2] <- readLists
  print $ minimum [2 * (d0 + d1),d0 + d1 + d2,2*(d0+d2),2*(d1+d2)]

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = solve =<< (map (fst.fromJust.C.readInt).C.words <$>C.getLine)
solve [x,y,z] = print $ minimum [2*(x+y), x+y+z, 2*(x+z), 2*(z+y)]

main=(print.fun.map read.words)=<<getLine where{fun[]=0;fun(a:b:c:xv)=f a b+f c(a + b)+f(g a b)(c+f a b) where{f c d|c>d=d;f c d = c;g c d|c<d=d;g c d = c;}}

countPath [d1, d2, d3] = [d1 + d2 + d3, 2*(d1 + d2), 2*(d1 + d3), 2*(d2 + d3)]

main :: IO ()
main = do
       ds <- getLine
       print $ minimum $ countPath $ map (\x -> read x::Int) $ words ds

import Control.Applicative
import Data.List

main = do
  [a,b,c]<- map read <$> words <$> getLine ::IO [Int]
  print $ minimum  [(a+b+c), (2*a+2*b), (2*a+2*c),(2*b+2*c)]

main :: IO ()
main = print . solve . map read . words =<< getContents

solve :: [Int] -> Int
solve [ d1, d2, d3 ] = minimum [ d1 + d2 + d3, 2 * (d1 + d2), 2 * (d2 + d3), 2 * (d3 + d1) ]
solve _ = undefined

main = interact (show . solve . map read . words)
solve [d1,d2,d3] = minimum [d1+d3+d2, 2*(d1+d2), 2*(d1+d3), 2*(d2+d3)]

module Main where
import Data.Functor

solve :: [Int] -> Int
solve [x,y,z] = minimum [2 * (x + z), 2*(y+z), 2 * (x + y), (x + y + z)]

main :: IO ()
main = print =<< (solve . map read . words <$> getLine)

main = interact $ show . solve . map read . words
solve [d1,d2,d3] = minimum [d1*2+d2*2, d1+d2+d3, d1*2+d3*2, d2*2+d3*2]

import Control.Applicative

main= do
	[d1,d2,d3]<- map read .words <$> getLine
	print $ minimum [ (2*d1+2*d2) ,(d1+d2+d3), 2*d2+2*d3, 2*d1+2*d3]

import Data.List (sort)

process:: [Int] -> Int
process ds = min (sum ds) (2 * (d1 + d2))
    where d1:d2:_ = sort ds

readInt :: String -> Int
readInt = read

main = do
    ds <- fmap (map readInt.words) getLine
    print $ process ds

max' [x] = x
max' (x:xs) = max x (max' xs)

main = do
  line <- getLine
  let [d1, d2, d3] = map read $ words line :: [Int]
  let d4 = min d1 (d2+d3)
  let d5 = min d2 (d1+d3)
  let d6 = min d3 (d1+d2)
  let ans = 2 * max' [d4, d5, d6]
  print ans

solve [d1, d2, d3] = min (d1 + d2 + d3) (2 * (d1 + d2))

main = getContents >>= putStrLn . show . solve . map read . words

main = do
  [a, b, c] <- fmap (map read . words) getLine

  print $ minimum [2*a+2*b, a+b+c]

--fast read
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Control.Applicative
readInts a = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.words a
readLists = readInts $ B.getLine
readLLists = fmap (map (fst . fromJust)) $ fmap (map B.readInt) $ fmap B.lines $ B.getContents
readMatrix = fmap readInts $ fmap B.lines $ B.getContents
main = do
  [d0,d1,d2] <- readLists
  print $ if d0 + d1 < d2 then 2 * (d0 + d1) else d0 + d1 + d2

import Data.List
import Data.Tree
import Data.Int
import Data.Maybe
import Data.Char
import Data.Functor
import Control.Monad
import Control.Monad.ST.Safe
import Control.Arrow
import Data.Array
import Data.Array.ST.Safe
import Data.STRef
import qualified Data.Map as Map
import qualified Data.Set as Set
import qualified Data.ByteString.Char8 as C
main=   readsolveprint
readsolveprint::IO()
readsolveprint = solve =<< (map (fst.fromJust.C.readInt).C.words <$>C.getLine)
solve [x,y,z] = print $ minimum [2*(x+y), x+y+z]

import Control.Applicative
import Data.List

main = do
  [a,b,c]<- map read <$> words <$> getLine ::IO [Int]
  print $ min (a+b+c) (2*a+2*b)

shortestRoute :: Int -> Int -> Int -> Int
shortestRoute d1 d2 d3 = min (2*d1 + 2*d2) (d1 + d2 + d3)

parseSpace :: String -> [Int]
parseSpace ws = map read (words ws) :: [Int]

main :: IO ()
main = do
  [d1, d2, d3] <- fmap parseSpace getLine
  print $ shortestRoute d1 d2 d3

import Control.Applicative

main= do
	[d1,d2,d3]<- map read .words <$> getLine
	print $ min (2*d1+2*d2) (d1+d2+d3)

process:: [Int] -> Int
process [d1, d2, d3]
    | d1 + d2 < d3 = 2 * (d1 + d2)
    | d2 + d3 < d1 = 2 * (d2 + d3)
    | d3 + d1 < d1 = 2 * (d3 + d1)
    | otherwise = d1 + d2 + d3

readInt :: String -> Int
readInt = read

main = do
    ds <- fmap (map readInt.words) getLine
    print $ process ds