N, M = gets.split.map(&:to_i)
E = $<.map{|ln| ln.split.map(&:to_i) }.sort_by{|l,r,|r}

C = [[1,0]]
E.each{|l,r,c|
	il = C.bsearch_index{|r,|l<=r}
	break unless il
	c += C[il][1]
	if C[-1][0]==r
		C[-1][1] = c if c < C[-1][1]
	else
		C << [r,c]
	end
}||(p(-1);exit)
C << [N,-1] if C[-1][0]!=N
p C[-1][1]
