import Shared
import Data.List
import System.Environment
import qualified Data.Map.Strict as M

main :: IO ()
main = getArgs >>= print . run . read . head

genTesting :: Integral a => a -> [[a]]
genTesting tar = concat $ map (helper . return) [2.. intSqrt tar]
    where helper (x:xs) = (x:xs) : 
                concatMap (\y -> helper $ y:x:xs) [x .. div tar (product (x:xs))]

calcK :: [Int] -> Int
calcK xs = product xs - sum xs + length xs

makeKMap :: Int -> M.Map Int Int
makeKMap = foldr helper M.empty . genTesting
        where helper xs = M.insertWith min (calcK xs) (product xs)

run :: Int -> Int
run n = sum $ nub $ map snd $ tail $ take n $ M.toList $ makeKMap $ 2*n
