// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_mut)]
#[allow(non_snake_case)]
fn main() {
    input!{
        N: usize,
    }


    let digit10 = [1_usize, 10, 100, 1000, 10000, 100000, 
                   1000000, 10000000, 100000000, 1000000000];
    let mut num3 = Vec::new();
    for digit in 1..10 {
        for p in 0..3_usize.pow(digit as u32){
            let mut num = p;
            let mut output = 0;
            for digit2 in 0..digit {
                output += [3, 5, 7][num % 3] * digit10[digit2];
                num /= 3;
            }
            num3.push(output);
        }
    }

    let mut num2 = Vec::new();
    for digit in 1..10 {
        for p in 0..2_usize.pow(digit as u32) {
            let mut num = p;
            let mut output35 = 0;
            let mut output57 = 0;
            let mut output73 = 0;
            for digit2 in 0..digit {
                output35 += [3, 5][num % 2] * digit10[digit2];
                output57 += [5, 7][num % 2] * digit10[digit2];
                output73 += [7, 3][num % 2] * digit10[digit2];
                num /= 2;
            }
            num2.push(output35);
            num2.push(output57);
            num2.push(output73);
        }
    }

    let mut num1 = Vec::new();
    for digit in 1..10 {
        let mut output3 = 0;
        let mut output5 = 0;
        let mut output7 = 0;
        for digit2 in 0..digit {
            output3 += 3 * digit10[digit2];
            output5 += 5 * digit10[digit2];
            output7 += 7 * digit10[digit2];
        }
        num1.push(output3);
        num1.push(output5);
        num1.push(output7);
    }

    let output = num3.iter().filter(|a| **a <= N).count() +
        num1.iter().filter(|a| **a <= N).count() -
        num2.iter().filter(|a| **a <= N).count();

    println!("{}", output);

}


Measured memory_bytes: 4352