def shuffle(x)
  ret = []
  ret_s = []
  while $sum + $card_s[$cnt] < x
    ret << $cards[$cnt]
    ret_s << $card_s[$cnt]
    $sum += $card_s[$cnt]
    $cnt += 1
  end
  t, b = $cards[$cnt]
  len = $card_s[$cnt]
  len1 = x - $sum
  m = t - 1 + len1
  ret << [t, m]
  ret_s << len1
  $sum += len1
  if len1 < len
    $cards[$cnt] = [m+1, b]
    $card_s[$cnt] = len - len1
  else $cnt += 1
  end
  [ret, ret_s]
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]]
  $card_s = [n]
  m.times do 
    x, y = gets.split.map(&:to_i)
    $sum = 0
    $cnt = 0
    top, top_s = shuffle(x)
    mid, mid_s = shuffle(y)
    btm = $cards[$cnt..-1]
    btm_s = $card_s[$cnt..-1]
    $cards = btm + mid + top
    $card_s = btm_s + mid_s + top_s
  end
  $sum = 0
  $cnt = 0
  top = shuffle(p-1)[0]
  mid = shuffle(q)[0]
  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