program nanagogo
  implicit none
  character(10) :: n, s
  character(1) :: a(3) = (/"7","5","3"/)
  integer :: x, l, m = 0
  read(*,*) n
  l = len_trim(n)
  read(n(1:l),*) x
  call dfs(l)
  write(*,'(i0)') m
contains
  recursive subroutine dfs(i)
    integer, intent(in) :: i
    integer :: v, k
    if (i == 0) return
    do k = 1, 3
      s(i:i) = a(k)
      read(s(i:l),*) v
      if (v <= x .and. check(s(i:l))) m = m+1
      call dfs(i-1)
    end do
  end
  logical function check(s)
    character(*), intent(in) :: s
    logical :: p(3)
    integer :: i
    p = .false.
    do i = 1, len(s)
      p = p .or. s(i:i) == a
    end do
    check = all(p)
  end
end program nanagogo