{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
import           Debug.Trace (trace)

main :: IO ()
main = do
  x <- readLn
  print $ solve x

solve :: Int -> Int
solve x = head $ filter isExpNumber [x, x-1 ..]

isExpNumber :: Int -> Bool
isExpNumber 1 = True
isExpNumber x = any f [2 .. x] where
  f b = go b 2
  go b p
   | x == y    = True
   | x < y     = False
   | otherwise = go b (p + 1) where
       y = b ^ p
