n,m=map(int,input().split()) z=[1,n] es=[] for i in range(m): a,b=map(int,input().split()) es+=[(a,b)] z+=[a,b] z=sorted(set(z)) d={v:i for i,v in enumerate(z)} l=len(z) e=[[] for i in range(l)] for i in range(l-1): e[i]+=[(i+1,(z[i+1]-z[i])*2)] for a,b in es: e[d[a]]+=[(d[b],2*b-2*a-1)] X=1<<60 v=[X]*l v[0]=0 q=[(v[0],0)] from heapq import heappush,heappop while len(q)>0: sc,sp=heappop(q) if sc>v[sp]: continue for tp,tc in e[sp]: if v[tp]>sc+tc: v[tp]=sc+tc heappush(q,(v[tp],tp)) print(v[-1])