#![allow(unused_imports)] #![allow(non_snake_case)] use std::cmp::*; use std::collections::*; use std::io::Write; #[allow(unused_macros)] macro_rules! debug { ($($e:expr),*) => { #[cfg(debug_assertions)] $({ let (e, mut err) = (stringify!($e), std::io::stderr()); writeln!(err, "{} = {:?}", e, $e).unwrap() })* }; } fn main() { let n = read::(); let mut ans = 0; for i in 10..min(100, n + 1) { if i % 3 == 0 && (i % 10) % 3 != 0 { ans += 1; } } let n = format!("{}", n); let mut dp = vec![vec![0i64; 2]; n.len() + 1]; dp[0][0] = 1; // pos, less for (i, c) in n.chars().enumerate() { for j in 0..2 { let lim = if j == 1 { 9 } else { c.to_digit(10).unwrap() }; for l in 0..lim + 1 { if l % 3 != 0 { continue; } let idx1 = if j == 1 || l < lim { 1 } else { 0 }; dp[i + 1][idx1] += dp[i][j]; } } } for j in 0..2 { ans += dp[n.len()][j]; } println!("{}", ans - 4); } fn read() -> T { let mut s = String::new(); std::io::stdin().read_line(&mut s).ok(); s.trim().parse().ok().unwrap() } fn read_vec() -> Vec { read::() .split_whitespace() .map(|e| e.parse().ok().unwrap()) .collect() }