N, M = gets.split.map(&:to_i) E = Hash.new { |h, k| h[k] = [] } P = [] M.times do a, b = gets.split.map(&:to_i) E[a] << b P << a P << b end P.unshift(1) P.push(N) P.uniq! P.sort! L = P.size dp = Array.new(L, Float::INFINITY) dp[0] = 0 I = Hash.new P.each_with_index do |pos, idx| I[pos] = idx end (L - 1).times do |i| a = P[i] n_pos = P[i + 1] dist = 2 * (n_pos - a) dp[i + 1] = dp[i] + dist if dp[i] + dist < dp[i + 1] E[a].each do |u| idx = I[u] b = P[idx] dist = 2 * b - 2 * a - 1 dp[idx] = dp[i] + dist if dp[i] + dist < dp[idx] end end puts dp.last