program main
  implicit none
  integer(8) :: a, b, c, d
  read (*, *) a, b, c, d
  write (*, "(i0)") g(b, c, d) - g(a - 1, c, d)
  contains

  pure integer(8) function g(x, c, d)
    integer(8), intent(in) :: x, c, d
    g = x - f(x, c) - f(x, d) + f(x, lcm(c, d))
  end function g

  pure integer(8) function f(x, y)
    integer(8), intent(in) :: x, y
    f = x / y
  end function f

  pure integer(8) function lcm(x, y)
    integer(8), intent(in) :: x, y
    lcm = x * (y / gcd(x, y))
  end function lcm

  pure function gcd(x, y) result(a)
    integer(8), intent(in) :: x, y
    integer(8) :: a, b, r
    a = max(x, y)
    b = min(x, y)
    do while (b > 0)
      r = mod(a, b)
      a = b
      b = r
    end do
  end function gcd
end program main
