program main
implicit None
	integer(8)::n,p,a(9),s(9,262143),b(262143),i,c,x,y,z,L,d,w
	
	read*,c
	
	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 (b(n) > c) 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
		endif
		n = n + 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