while n=gets.chomp.to_i
	break if n==0


	oldnum=0
	newnum=0

	while oldnum <= n
		if newnum.to_s.include?("4") || newnum.to_s.include?("6") 
			newnum+=1
		else
			oldnum+=1
			newnum+=1
		end
	end

		p newnum-1
end