Buggy python submission (verdict: Time Limit Exceeded):
L, N = list(map(int, input().split()))
X = [int(input()) for i in range(N)]

## Y[i] = X[i+1]-X[i]
#Y = [X[i+1]-X[i] if i<N-1 else L+X[0]-X[N-1] for i in range(N)]
#print(Y)

''' Start at X[0] '''

#''' In the case when turn first '''
#ans0 = X[0]
## 0, -1, 1, -2, ...
#i = 0
#j = N-1
#direc = 1 # direction: left->1, right->-1
#while i!=j:
#    print(i, j)
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[1]
#i = 1
#j = N-1
#direc = 1 # direction: left->1, right->-1
#while i!=j:
#    print(i, j)
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[2]
#i = 2
#j = N-1
#direc = 1 # direction: left->1, right->-1
#while i!=j:
#    print(i, j)
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
## 0, -1, 1, -2, 2, ...
## 0, 1, -1, 2, -2, 3, ...

ans0 = 0
for k in range(N):
    tmp_ans = X[k]
    i = k
    j = N-1
    direc = 1 # direction: left->1, right->-1
    while i!=j:
        direc *= -1
        tmp_ans += L+X[i]-X[j]
        if direc==-1:
            i += 1
        elif direc==1:
            j -= 1
    if ans0<tmp_ans:
        ans0 = tmp_ans

ans1 = 0
for k in reversed(range(N)):
    tmp_ans = L-X[k]
    i = 0
    j = k
    direc = -1 # direction: left->1, right->-1
    while i!=j:
        direc *= -1
        tmp_ans += L+X[i]-X[j]
        if direc==-1:
            i += 1
        elif direc==1:
            j -= 1
    if ans1<tmp_ans:
        ans1 = tmp_ans

print(max(ans0, ans1))


Fixed submission:
L, N = list(map(int, input().split()))
X = [int(input()) for i in range(N)]

## Y[i] = X[i+1]-X[i]
#Y = [X[i+1]-X[i] if i<N-1 else L+X[0]-X[N-1] for i in range(N)]
#print(Y)

''' Start at X[0] '''
''' In the case when turn first '''
ans0 = X[0]
# 0, -1, 1, -2, ...
i = 0
j = N-1
direc = 1 # direction: left->1, right->-1
while i!=j:
    direc *= -1
    ans0 += L+X[i]-X[j]
    if direc==-1:
        i += 1
    elif direc==1:
        j -= 1
if N%2==0:
    i = N//2
    j = N//2
else:
    i = N//2
    j = N//2+1
tmp_ans = ans0
for k in range(N-1):
    tmp_ans += -L-2*X[k]+X[i]+X[j]
    if i==j:
        j += 1
    else:
        i += 1
    ans0 = max(ans0, tmp_ans)


''' Start at X[N-1] '''
''' In the case when turn first '''
ans1 = L-X[N-1]
i = 0
j = N-1
direc = -1 # direction: left->1, right->-1
while i!=j:
    direc *= -1
    ans1 += L+X[i]-X[j]
    if direc==-1:
        i += 1
    elif direc==1:
        j -= 1
if N%2==0:
    i = (N-2)//2
    j = (N-2)//2
else:
    i = (N-2)//2
    j = (N-2)//2+1
tmp_ans = ans1
for k in reversed(range(1, N)):
    tmp_ans += -L+2*X[k]-X[i]-X[j]
    if i==j:
        i -= 1
    else:
        j -= 1
    ans1 = max(ans1, tmp_ans)

print(max(ans0, ans1))

#ans0 = X[0]
## 0, -1, 1, -2, ...
#i = 0
#j = N-1
#direc = 1 # direction: left->1, right->-1
#print('X[0]')
#while i!=j:
#    print('L+X[%d]-X[%d]' % (i, j))
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[1]
#i = 1
#j = N-1
#direc = 1 # direction: left->1, right->-1
#print('X[1]')
#while i!=j:
#    print('L+X[%d]-X[%d]' % (i, j))
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[2]
#i = 2
#j = N-1
#direc = 1 # direction: left->1, right->-1
#print('X[2]')
#while i!=j:
#    print('L+X[%d]-X[%d]' % (i, j))
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[3]
#i = 3
#j = N-1
#direc = 1 # direction: left->1, right->-1
#print('X[3]')
#while i!=j:
#    print('L+X[%d]-X[%d]' % (i, j))
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
#
#ans0 = X[4]
#i = 4
#j = N-1
#direc = 1 # direction: left->1, right->-1
#print('X[4]')
#while i!=j:
#    print('L+X[%d]-X[%d]' % (i, j))
#    direc *= -1
#    ans0 += L+X[i]-X[j]
#    if direc==-1:
#        i += 1
#    elif direc==1:
#        j -= 1
#print(ans0)
## 0, -1, 1, -2, 2, ...
## 0, 1, -1, 2, -2, 3, ...



''' N^2 case '''
#ans0 = 0
#for k in range(N):
#    tmp_ans = X[k]
#    i = k
#    j = N-1
#    direc = 1 # direction: left->1, right->-1
#    while i!=j:
#        direc *= -1
#        tmp_ans += L+X[i]-X[j]
#        if direc==-1:
#            i += 1
#        elif direc==1:
#            j -= 1
#    if ans0<tmp_ans:
#        ans0 = tmp_ans
#
#ans1 = 0
#for k in reversed(range(N)):
#    tmp_ans = L-X[k]
#    i = 0
#    j = k
#    direc = -1 # direction: left->1, right->-1
#    print("L-X[%d]" % k)
#    while i!=j:
#        print("L+X[%d]-X[%d]" % (i, j))
#        direc *= -1
#        tmp_ans += L+X[i]-X[j]
#        if direc==-1:
#            i += 1
#        elif direc==1:
#            j -= 1
#    if ans1<tmp_ans:
#        ans1 = tmp_ans
#    print('='*80)
#
#print(max(ans0, ans1))