      MODULE gcdMod
! 最小公倍数GCDを求めるモジュール
! ユークリッドの互除法を使用する．
      IMPLICIT NONE
      
      interface gcd
        module procedure :: intGcd
      end interface
      
      integer(16),parameter,public :: precision = 16
      
      contains
      function intGcd( a,b ) result( gcd )
        implicit none
        integer(precision),intent(in) :: a,b
        integer(precision) :: gcd, smaller, bigger, remain
        
        bigger  = max( a,b )
        smaller = min( a,b )
        
        do while(.true.)
          remain = mod( bigger,smaller )
          if( remain==0 )then
            gcd = smaller; return
          else
            bigger  = smaller
            smaller = remain
          end if
        end do
      end function
      end MODULE gcdMod

      MODULE lcmMod
! 最小公倍数LCMを求めるモジュール
! ユークリッドの互除法によるGCDを使用する．
      
      use gcdMod
      implicit none
      
      interface lcm
        module procedure :: intLcm
      end interface
      
      contains
      function intLCM( a,b ) result( lcm )
        implicit none
        integer(precision),intent(in) :: a,b
        integer(precision) :: lcm
        
        lcm = a*b/gcd(a,b)
      end function
      end MODULE lcmMod
      


      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

      PROGRAM AntiDivision
      use lcmMod
      IMPLICIT NONE
      integer(16) :: a,b,c,d
      integer(16) :: ac,ad,acd,bc,bd,bcd
      integer(16) :: ansA,ansB
      
      read*,a,b,c,d
      
      a = a-1
      
      ac = a/c
      ad = a/d
      acd = a / lcm(c,d)
      
      ansA = a - (ac+ad-acd)
      
      bc = b/c
      bd = b/d
      bcd = b / lcm(c,d)
      
      ansB = b - (bc+bd-bcd)
      
      print*,ansB - ansA
      
      
      
      END PROGRAM