Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = 0
done = [A[0]]
not_done = A[1:N]

while True:
    min_d = 1e6
    for a in done:
        for b in range(len(not_done)):
            if a>b:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]-K))
            else:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]+K))
            min_d = min(d, min_d)
            min_idx = b
    dis += min_d
    done.append(not_done.pop(min_idx))
    if len(not_done) ==0:
        break
print(dis)


Fixed submission:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)

Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = 0
done = [A[0]]
not_done = A[1:N]

while True:
    min_d = 1e6
    for a in done:
        for b in range(len(not_done)):
            if a>b:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]-20))
            else:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]+20))
            min_d = min(d, min_d)
            min_idx = b
    dis += min_d
    done.append(not_done.pop(min_idx))
    if len(not_done) ==0:
        break
print(dis)


Fixed submission:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)

Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = 0
done = [A[0]]
not_done = A[1:N]

while True:
    min_d = 1e6
    for a in done:
        for b in range(len(not_done)):
            if a>b:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]-20))
            else:
                d = min(abs(a-not_done[b]), abs(a-not_done[b]+20))
            min_d = min(d, min_d)
            min_idx = b
    dis += min_d
    done.append(not_done.pop(b))
    if len(not_done) ==0:
        break
print(dis)


Fixed submission:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)

Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = 0

a = A[0]
for i in range(len(A)-1):
    min_d = 1e6
    for i in range(1,len(A)-1):
        if a>A[i]:
            d = min(abs(a-A[i]), abs(a-A[i]-20))
        else:
            d = min(abs(a-A[i]), abs(a-A[i]+20))
        min_d = min(d, min_d)
    dis += min_d
print(dis)


Fixed submission:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)

Buggy python submission (verdict: Time Limit Exceeded):
K, N = map(int, input().split())
A = list(map(int, input().split()))

dis = 0

n = len(A)
idx = 0
a = A[0]
while n>1:
    min_idx = 0
    min_d = 1e6
    for i in range(len(A)):
        if not idx == i:
            if a>A[i]:
                d = min(abs(a-A[i]), abs(a-A[i]-20))
            else:
                d = min(abs(a-A[i]), abs(a-A[i]+20))
            if d<min_d:
                min_d = d
                min_idx = i
    a = A[i]
    idx = i
    dis += min_d
    n-=1
print(dis)


Fixed submission:
import numpy as np

K, N = map(int, input().split())
A = list(map(int, input().split()))

A.append(K+A[0])
l = 0
for i in range(N):
    l = max(l, A[i+1]-A[i])
print(K-l)