Buggy python submission (verdict: Time Limit Exceeded):
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]
s = np.sum(dist)
result = []
for start_index in min_index:
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))


Fixed submission:
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

#min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))

Buggy python submission (verdict: Time Limit Exceeded):
k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]

right = 0
left = 0
result = []
for start_index in min_index:
  for n in range(0, len(dist)-1):
    x = start_index + n
    y = start_index - n
    if x > len(dist)-1:
      x = (len(dist) - n - start_index) * -1
    if y < 0:
      y = len(dist)-n
    right += dist[x]
    left += dist[y]
    if len(result) > 0 and (min(result) < right and min(result) < left):
      break
  result.append(min(right, left))
  
print(min(result))

Fixed submission:
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

#min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))

Buggy python submission (verdict: Time Limit Exceeded):
k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)

min_index = [i for i, v in enumerate(dist) if v == min(dist)]

right = 0
left = 0
result = []
for start_index in min_index:
  for n in range(0, len(dist)-1):
    x = start_index + n
    y = start_index - n
    if x > len(dist)-1:
      x = (len(dist) - n - start_index) * -1
    if y < 0:
      y = len(dist)-n
    right += dist[x]
    left += dist[y]
  result.append(min(right, left))
  
print(min(result))

Fixed submission:
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

#min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))

Buggy python submission (verdict: Time Limit Exceeded):
k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append([k-a[len(a)-1]+a[0], a[1]-a[0]])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append([a[num]-a[num-1], k-a[num]+a[0]])
  else:
  	dist.append([a[num]-a[num-1], a[num+1]-a[num]])
#print(dist)

start = 0
result = []
while(start!=len(dist)):
  right = 0
  left = 0
  for x in range(0, len(dist)-1):
    n = start+x
    if n > len(dist)-1:
      n = (len(dist) - x - start) * -1
    right += dist[n][1]
  for y in range(0, len(dist)-1):
    n = start-y
    if n < 0:
      n = len(dist)-y
    left += dist[n][0]
  result.append(min(right, left))
#  print(right, left)
  start += 1
  
print(min(result))

Fixed submission:
import numpy as np

k, n = map(int, input().split())
a = list(map(int, input().split())) 

dist = []
dist.append(a[1]-a[0])

for num in range(1, len(a)):
  if num == len(a) - 1:
    dist.append(k-a[num]+a[0])
  else:
    dist.append(a[num+1]-a[num])
#print(dist)
dist = np.array(dist)

#min_index = np.where(dist == dist.min())[0]
s = np.sum(dist)
result = []
for start_index in range(0, len(dist)):
  if start_index == 0:
    result.append(s - dist[len(dist)-1])
    result.append(s - dist[start_index+1])
  elif start_index == len(dist)-1:
    result.append(s - dist[start_index-1])
    result.append(s - dist[0])
  else:
    result.append(s - dist[start_index-1])
    result.append(s - dist[start_index+1])
print(min(result))