{-# LANGUAGE BangPatterns #-}

import Data.Bits
import Data.List
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IntMap
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Data.Coerce

newtype BitFlag = BF Int

initFlag :: BitFlag
initFlag = BF (bit 3 .|. bit 5 .|. bit 7)

{-# INLINE lenFlag #-}
lenFlag :: BitFlag -> Int
lenFlag = coerce (popCount :: Int -> Int)

{-# INLINE testFlag #-}
testFlag :: Int -> BitFlag -> Bool
testFlag = coerce (flip testBit :: Int -> Int -> Bool)

{-# INLINE killFlag #-}
killFlag :: Int -> BitFlag -> BitFlag
killFlag = coerce (flip clearBit :: Int -> Int -> Int)
  
gen :: Int -> BitFlag -> Int -> Int -> (Int,Bool)
gen 0 _ till acc
  | acc <= till = (1,True)
  | otherwise   = (0,False)
gen n !xs till !acc = foldr count out [3,5..7] 0
  where
    count | lenFlag xs < n
            = \ !k cont !left ->
                let (!now, hasMore)
                      = gen (n-1) (killFlag k xs) till (10*acc + k) 
                in if hasMore then
                     cont (left + now)
                   else
                     (left + now, False)
          | otherwise
            = \ !k cont !left ->
              if not (testFlag k xs) then
                cont left
              else let (!now,hasMore)
                         = gen (n-1) (killFlag k xs) till (10*acc + k)
                   in if hasMore then
                        cont (left + now)
                      else
                        (left + now, False)
    out !left = (left, True)
  
cNum :: Int -> Int
cNum !till = foldr count out [3,4..9] 0
  where
    count !n cont !left = if hasMore then
                            cont (left + now)
                          else
                            left + now
      where
        (!now, hasMore) = gen n initFlag till 0
    out = id

main :: IO ()
main = getLine >>= print . cNum . read 
