#方針ーできるだけ計算を省きながら全探索する。n=10^9までなので、頑張れば行ける
# 一桁目が3,5,7以外　＝＞ 即死
# 0,2,4,6,8は偶数で切れる(前回取得していた場合は必ず偶数になる)
# 100の位が1 => 100ならば200までは候補ならない＝＞飛ばす
n = gets.to_i
ans = 0
last_time_get = false
dev10 = 0
able_get = false
next_count = 0
count = 0
numc = 0
while(numc <= n)
  num = numc
  #ぶっ飛ばししょり
  dev100000000 = num % 100000000
  if dev100000000 == 0
    case (num / 100000000)%10
    when 1,2,4,6,8,9
    next_count = 100000000
    end
  end
  #ぶっ飛ばししょり
  dev10000000 = num % 10000000
  if dev10000000 == 0
    case (num/10000000)%10
    when 1,2,4,6,8,9
    next_count = 10000000
    end
  end
  #ぶっ飛ばししょり
  dev1000000 = num % 1000000
  if dev1000000 == 0
    case (num/1000000)%10
    when 1,2,4,6,8,9
    next_count = 1000000
    end
  end
  #ぶっ飛ばししょり
  dev100000 = num % 100000
  if dev100000 == 0
    case (num/100000)%10
    when 1,2,4,6,8,9
    next_count = 100000
    end
  end
  #ぶっ飛ばししょり
  dev10000 = num % 10000
  if dev10000 == 0
    case (num/10000)%10
    when 1,2,4,6,8,9
    next_count = 10000
    end
  end
  #ぶっ飛ばししょり
  dev1000 = num % 1000
  if dev1000 == 0
    case (num/1000)%10
    when 1,2,4,6,8,9
    next_count = 1000
    end
  end
  #ぶっ飛ばししょり
  dev100 = num % 100
  if dev100 == 0
    case (num/100)%10
    when 1,2,4,6,8,9
    next_count = 100
    end
  end
  #ぶっ飛ばししょり
  dev = num % 10
  if dev == 0
    case (num/10)%10
    when 1,2,4,6,8,9
    next_count = 10
    end
  end
  if next_count > 0
    numc += next_count
    next_count = 0
    next
  end

    count += 1

  if last_time_get
    last_time_get = false
    numc += 1
    next
  end
  if num % 2 == 0
    numc += 1
    next
  end
  if num == 1
    numc += 1
    next
  end
  able_get = true
  is3 = false
  is5 = false
  is7 = false
  while num != 0
    dev10 = num % 10
    if dev10 == 1 or dev10 == 9 or dev10 == 0 or dev10 == 2 or dev10 == 4 or dev10 == 6 or dev10 == 8
      able_get = false
      break
    end
    is3 = true if dev10 == 3
    is5 = true if dev10 == 5
    is7 = true if dev10 == 7
    num -= dev10
    num /= 10
  end
  if able_get && is7 && is5 && is3
    ans += 1
    last_time_get = true
  end

  numc += 1
end

puts ans.to_s