n, m = gets.split.map(&:to_i)

def ranges_overlap?(a, b)
  (a.begin < b.begin && a.end < b.end) || (a.begin  b.begin && a.end > b.end)
end

def merge_ranges(a, b)
  [a.begin, b.begin].max..[a.end, b.end].min
end


ary = []
m.times do |i|
 if i == 0
   range = gets.split.map(&:to_i)
   ary = (range[0]..range[1])
 else
   range = gets.split.map(&:to_i)
   if ranges_overlap?(ary, (range[0]..range[1]))
     ary = merge_ranges(ary, (range[0]..range[1]))
   end
 end
end
p ary.size