module Main (main) where

import Data.List
import System.Environment

comb :: Int -> [a] -> [([a],[a])]
comb 0 xs = [([],xs)]
comb _ [] = []
comb k (x:xs) = [ (x:ys,zs) | (ys,zs) <- comb (k-1) xs ]
              ++[ (ys,x:zs) | (ys,zs) <- comb k     xs ]

comb' :: Int -> Int -> [Int] -> [([Int],[Int])]
comb' s 0 xs | s == 0    = [([],xs)]
             | otherwise = []
comb' _ _ []             = []
comb' s k (x:xs) = [ (x:ys,zs) | (ys,zs) <- comb (k-1) xs, s == x+sum ys ]

foo s k (acc,xs) = case unzip $ comb' s k xs of
  (yss,zss) -> zip (map (:acc) yss) zss
            
magic n = map (reverse . fst) $ iterate (concatMap (foo s n)) [([],ns)] !! n
  where ns = [1..n^2]
        s  = sum ns `div` n

main = print . length . magic . read . head =<< getArgs
