def shuffle(x, cnt, sum)
  ret = []
  while sum + $cards[cnt][2] < x
    ret << $cards[cnt]
    sum += $cards[cnt][2]
    cnt += 1
  end
  t, b, len = $cards[cnt]
  len1 = x - sum
  m = t - 1 + len1
  ret << [t, m, len1]
  sum += len1
  if len1 < len
    $cards[cnt] = [m+1, b, len - len1]
  else cnt += 1
  end
  [ret, cnt, sum]
end

def shuffle2(x, y)
  btm, cnt, sum = shuffle(x, 0, 0)
  mid, cnt, sum = shuffle(y, cnt, sum)
  $cards[cnt..-1] + mid + btm
end

while true
  n = gets.to_i
  break if n == 0
  m = gets.to_i
  p, q, r = gets.split.map(&:to_i)
  $cards = [[1,n,n]]
  m.times do 
    x, y = gets.split.map(&:to_i)
    $cards = shuffle2(x, y)
  end
  top, cnt, sum = shuffle(p-1, 0, 0)
  mid, cnt, sum = shuffle(q, cnt, sum)
  ret = 0
  for cds in mid
    if cds[0] <= r
      ret += (cds[1] <= r ? cds[1] : r) - cds[0] + 1
    end
  end
  p ret
end