LRC = $<.map{|ln| ln.split.map(&:to_i) }
N,M = LRC.shift

RC = [[1,0]]
LRC.sort_by{|_,r| r }.each{|l,r,c|
	i = (0...RC.size).bsearch{|i| l<=RC[i][0] }||next
	c += RC[i][1]
	j = (i...RC.size).bsearch{|j| c<RC[j][1] }
	RC.pop(RC.size-j) if j
	RC.push([r,c]) if RC[-1][0] < r
}

r,c = RC[-1]
p(r<N ? -1 : c)
