let (>>=) xs f = List.map f xs |> List.concat
let rec replicate_m n xs =
  if n <= 0 then [[]]
  else xs >>= fun x -> List.map (fun y -> x::y) (replicate_m (n-1) xs)

let is_753 =
  let rec aux a b c x =
    if x <= 0 then a && b && c
    else
      match x mod 10 with
      | 3 -> aux true b c (x/10)
      | 5 -> aux a true c (x/10)
      | 7 -> aux a b true (x/10)
      | _ -> false
  in aux false false false

let () =
  Scanf.scanf "%d" @@ fun n ->
  replicate_m 9 [0; 3; 5; 7]
  |> List.fold_left (fun s xs ->
    let v = List.fold_left (fun a b -> 10*a + b) 0 xs in
    if v <= n && is_753 v then s+1 else s) 0
  |> Printf.printf "%d\n"
