Problem:
Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484

Solution in Fortran (gfortran v4.8.4) (judge verdict: Runtime Error)

program main
implicit None
	integer(8)::n,p,a(9),s(9,262143),b(262143),i,c,x,y,z,L,d,w
	integer(8)::r(26484)
	
	do n = 1,262143
		call tet(n,a)
		s(:,n) = a
	enddo
	
	s = (1-((8-(s*2+1))/7))*(s*2+1)
	
	b = 0
	do n = 1,262143
		do i = 1,9
			b(n) = b(n) + s(i,n)*(10**(i-1))
		enddo
	enddo
	
	p = 0
	n = 1
	
	do
		if (p > 26484) exit
		x = 0;y = 0;z = 0;w = 1
		d = b(n)
		L = int(log10(real(d)))+1
		do i = L,1,-1
			if(d/10**(i-1) == 7)then
				x =1
				d = d - 7 * 10**(i-1)
				else if(d/10**(i-1) == 5)then
				y =1
				d = d - 5 * 10**(i-1)
				else if(d/10**(i-1) == 3)then
				z =1
				d = d - 3 * 10**(i-1)
				else if(d/10**(i-1) == 0)then
				w = 0
				exit
			endif
		enddo
		
		!print 101,b(n),L,x,y,z,w
		if(x ==1 .and. y == 1 .and. z ==1 .and. w == 1)then
			p = p + 1
			r(p) = b(n)
		endif
		n = n + 1
	enddo
	
	read*,c
	
	
	p = 0
	do
		if(p == 26484) exit
		if(r(p+1) > c) exit
		if(r(p+1) < c) p = p + 1
	enddo
	
	print *,p
	
101 format(8(i0,1x))
end program main

subroutine tet(n,a)
	integer(8)::n,i,p
	integer(8)::a(9)
	
	a = 0
	p = n
	i = 1
	do while (p /= 0)
		a(i) = mod(p,4)
		p = p/4
		i = i + 1
	enddo
end subroutine tet