require 'matrix'
N, M = gets.split.map!(&:to_i)
count = 0
data = []
while l, r = gets.split.map!(&:to_i)
	a = [l, r] if count == 0
	a[0] = l if a[0] < l
	a[1] = r if r < a[0]
	count += 1
end
puts a[1] - a[0]