Buggy python submission (verdict: Time Limit Exceeded):
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[a.index(i)+1]-i)<=k-(i-a[a.index(i)-1]):
                res=k-(a[a.index(i)+1]-i)
            else:
                res=k-(i-a[a.index(i)-1])
        if resf>=res:
            resf=res
    print(resf)

if __name__ == '__main__':
    main()

Fixed submission:
# coding: utf-8
# Your code here!
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            res=min(k-(a[a.index(i)+1]-i),k-(i-a[a.index(i)-1]))
        resf=min(resf,res)
    print(resf)

if __name__ == '__main__':
    main()

Fixed submission:
# coding: utf-8
# Your code here!
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            res=min(k-(a[a.index(i)+1]-i),k-(i-a[a.index(i)-1]))
        resf=min(resf,res)
    print(resf)

if __name__ == '__main__':
    main()

Fixed submission:
# coding: utf-8
# Your code here!
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
k,n=map(int,input().split())
a=list(map(int,input().split()))
resf=10**8
for i in a:
    if i==a[0]:
        res=min(a[-1]-a[0],k-(a[1]-a[0]))
    elif i==a[-1]:
        res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
    else:
        res=min(k-(a[a.index(i)+1]-i),k-(i-a[a.index(i)-1]))
    resf=min(resf,res)
print(resf)


Fixed submission:
# coding: utf-8
# Your code here!
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
k,n=map(int,input().split())
a=list(map(int,input().split()))
resf=10**8
for i in a:
    if i==a[0]:
        res=min(a[-1]-a[0],k-(a[1]-a[0]))
    elif i==a[-1]:
        res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
    else:
        res=min(k-(a[a.index(i)+1]-i),k-(i-a[a.index(i)-1]))
    resf=min(resf,res)
print(resf)

Fixed submission:
# coding: utf-8
# Your code here!
def main():
    k,n=map(int,input().split())
    a=list(map(int,input().split()))
    resf=10**7
    z=0
    for i in a:
        if i==a[0]:
            res=min(a[-1]-a[0],k-(a[1]-a[0]))
        elif i==a[-1]:
            res=min(k-(a[-1]-a[-2]),a[-1]-a[0])
        else:
            if k-(a[z+1]-i)<=k-(i-a[z-1]):
                res=k-(a[z+1]-i)
            else:
                res=k-(i-a[z-1])
        if resf>=res:
            resf=res
        z+=1
    print(resf)

if __name__ == '__main__':
    main()