function GCD() {
  m=$1 n=$2
  if [[ $((m>=n)) = 0 ]]; then
    ((tmp=n,n=m,m=tmp))
  fi
  while [[ $((n==0)) = 0 ]]; do
    ((tmp=n,n=m%n,m=tmp))
  done
  echo $m
}

function LCM() {
  m=$1 n=$2
  echo $(((m*n)/$(GCD $m $n)))
}

read A B C D

echo $((
  A--,
  lcm=$(LCM $C $D),
  (B-A)-((B/C-A/C)+(B/D-A/D)-(B/lcm-A/lcm))
))