L,N=gets.split.map &:to_i
X=[*1..N].map{gets.to_i}
now=0
def dist(n,obj,f)
    maxL=1000000001
    if f==1 then
        return [obj-n<0? maxL: obj-n,L+obj-n<0? maxL: L+obj-n].min
    else 
        return [n-obj<0? maxL: n-obj,L+n-obj<0? maxL: L+n-obj].min
    end
end
def solve(s,n,x)
    if x.size==1 then return s+[dist(n,x[0],0),dist(n,x[0],1)].max end
    if dist(n,x.first,1)>dist(n,x.last,0) then
        xp=eval x.to_s
        xp.shift
        return solve(dist(n,x.first,1)+s,n=x.first,xp)
    elsif dist(n,x.first,1)<dist(n,x.last,0) then
        xp=eval x.to_s
        xp.pop
        return solve(dist(n,x.last,0)+s,n=x.last,xp)
    else
        xp1=eval x.to_s
        xp2=eval x.to_s
        xp1.shift
        xp2.pop
        return [solve(dist(n,x.first,1)+s,x.first,xp1),solve(dist(n,x.last,0)+s,x.last,xp2)].max
    end
end
xp1=eval X.to_s
xp2=eval X.to_s
xp1.shift
xp2.pop
p [solve(dist(0,X.first,1),X.first,xp1),solve(dist(0,X.last,0),X.last,xp2)].max
