qyliang/CodeNanoFix huggingface dataset:
# Buggy code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def make_divisors(n):
    divisors = []
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
n = int(input())
if n == 2:
    print(0)
elif n == 3:
    print(2)
elif n == 4:
    print(3)
else:
    count = 2
    check_list = make_divisors(n-1)
    count += len(check_list)
    temp_n = str(n)
    suma = 0
    for i in range(len(temp_n)):
        suma += int(temp_n[i])
    if suma % 3 == 0:
        count += 1
        
    """
    for k in [3]:
        temp_n = n
        while(True):
            if temp_n % k == 0:
                temp_n = temp_n // k
            else:
                temp_n = temp_n - k
                if temp_n <= 0:
                    break
            if temp_n == 1:
                count += 1
                break
    """
    print(count)


# Fixed code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def make_divisors(n):
    divisors = []
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    # divisors.sort()
    return divisors
n = int(input())
if n == 2:
    print(1)
elif n == 3:
    print(2)
elif n == 4:
    print(3)
else:
    count = 2
    check_list = make_divisors(n-1)
    count += len(check_list)
    check_list = make_divisors(n)
    for k in check_list:
        temp_n = n // k
        while(True):
            if temp_n % k == 0:
                temp_n = temp_n // k
            elif temp_n % k == 1:
                count += 1
                break
            else:
                break
    print(count)
# Buggy code:
N = int(input())
A = []
c = 0

for i in range(2,int(N**0.5)+2):
    if N % i == 0:
        A.append(i)
        A.append(N//i)

for i in range(2,int((N-1)**0.5)+1):
    if (N - 1) % i == 0:
        c += 2

if N == 2:
    print(2)
else:
    for i in A:
        start = N
        while(True):
            if start % i != 0:
                break
            start = start//i
            if start % i == 1:
                c += 1
                break
    print(c+2)

# Fixed code:
N = int(input())
A = []
c = 0

for i in range(2,int(N**0.5)+1):
    if N % i == 0:
        A.append(i)
        A.append(N//i)

for i in range(2,int((N-1)**0.5)+1):
    if (N - 1) % i == 0:
        A.append(i)

        A.append((N-1)//i)
A = list(set(A))
if N == 2:
    print(1)
else:
    for i in A:
        start = N
        while(True):
            if start % i != 0:
                break
            start = start//i
        if start % i == 1:
            c += 1
    print(c+2)
# Buggy code:
n=int(input())
def prime_factorize(n):
    a=[]
    while n%2==0:
        a.append(2)
        n//=2
    f=3
    while f**2<=n:
        if n%f==0:
            a.append(f)
            n//=f
        else:
            f+=2
    if n!=1:
        a.append(n)
    return(a)

import collections
A=collections.Counter(prime_factorize(n-1))
B=collections.Counter(prime_factorize(n))

ans=1
cnt=0
for a,b in A.items():
    ans*=(b+1)
for a, b in B.items():
    if b>=2:
        for i in range(2,b+1):
            if (n//(a**i))%a==1:
                cnt+=1
import math
for i in range(1,math.floor(n**0.5)+1):
    if n%i==0:
        if ((n//i)-1)%i==0:
            cnt+=1
        if ((n//i)-1)%(n//i)==0:
            cnt+=1

print(ans-2+cnt)



# Fixed code:
n=int(input())
def prime_factorize(n):
    a=[]
    while n%2==0:
        a.append(2)
        n//=2
    f=3
    while f**2<=n:
        if n%f==0:
            a.append(f)
            n//=f
        else:
            f+=2
    if n!=1:
        a.append(n)
    return(a)

import collections
A=collections.Counter(prime_factorize(n-1))
ans=1
cnt=0
for a,b in A.items():
    ans*=(b+1)

import math
li=[]
for i in range(2,math.floor(n**0.5)+1):
    if n%i==0:
        li.append(i)
        if i!=n//i:
            li.append(n//i)
li.append(n)
for i in li:
    x = n
    while x % i == 0:
        x //= i
    if x % i == 1:
        cnt += 1
print(ans - 1 + cnt)
# Buggy code:
from sys import stdin
from math import sqrt
input = stdin.readline

N = int(input())

s = set([N - 1, N])


for i in range(2, int(sqrt(N)) + 1):
    tmp = N
    if (tmp - 1) % i == 0:
        s.add(i)
        s.add((tmp - 1) // i)
    elif tmp % i == 0:
        while tmp % i == 0:
            tmp //= i

        if tmp % i == 1:
            s.add(i)

        tmp = N
        while tmp % (N // i) == 0:
            tmp //= (N // i)
            print(tmp)

        if tmp % (N // i) == 1:
            s.add(tmp // i)

# print(s)
print(len(s))


# Fixed code:
from sys import stdin
from math import sqrt
input = stdin.readline

N = int(input())

if N == 2:
    print(1)
    exit()

s = set([N - 1, N])


for i in range(2, int(sqrt(N)) + 1):
    tmp = N
    if (tmp - 1) % i == 0:
        s.add(i)
        s.add((tmp - 1) // i)
    elif tmp % i == 0:
        while tmp % i == 0:
            tmp //= i

        if tmp % i == 1:
            s.add(i)

        tmp = N
        while tmp % (N // i) == 0:
            tmp //= (N // i)

        if tmp % (N // i) == 1:
            s.add(N // i)

# print(s)
print(len(s))
# Buggy code:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

divlist = make_divisors(N)
count = 0
if len(divlist) != 2:
    for i in divlist[2:]:
        temp = N // i
        if temp % i == 1:
            count += 1

print(count + len(make_divisors(N-1)))

# Fixed code:
N = int(input())

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors

divlist = make_divisors(N)
count = 0
for i in divlist[1:]:
    n = N
    while n % i == 0:
        n = n // i
    if n % i == 1:
        count += 1

print(count + len(make_divisors(N-1))-1)
# Buggy code:
n = int(input())

def divisior(n):
    a = []
    i = 1
    while (i*i <= n):
        if n%i == 0:
            a.append(i)
            if (i*i != n): a.append(n//i)
        i += 1

    return a

ans = 0
a = divisior(n).sort()

for x in a[1:]:
    tmp = n
    while (tmp%x == 0): tmp = tmp//x
    if (tmp%x == 1) : ans += 1

b = divisior(n-1)

ans = ans + len(b) -1

print(ans)




# Fixed code:
n = int(input())

def divisior(n):
    a = []
    i = 1
    while (i*i <= n):
        if n%i == 0:
            a.append(i)
            if (i*i != n): a.append(n//i)
        i += 1
    return a

ans = 0
a = divisior(n)

for x in a[1:]:
    tmp = n
    while (tmp%x == 0): tmp = tmp//x
    if (tmp%x == 1) : ans += 1

b = divisior(n-1)

ans = ans + len(b) -1

print(ans)
# Buggy code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def get_yakusu(n):
    ans = set()
    for i in range(1, int(math.sqrt(n)) + 2):
        if n % i == 0:
            if i != 1:
                ans.add(i)
            if n // i != i:
                ans.add(n // i)
    return ans

def main():
    N = I()
    cnt = 0
    yakusu = get_yakusu(N)
    n1yakusu = get_yakusu(N-1)
    for n in yakusu:
        cpN = N
        while cpN % n == 0 and cpN > n:
            cpN //= n
        if cpN % n == 1:
            cnt += 1
    cnt += len(n1yakusu)
    print(cnt)
main()



# Fixed code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

def get_yakusu(n):
    ans = set()
    for i in range(1, int(math.sqrt(n)) + 1):
        if n % i == 0:
            if i != 1:
                ans.add(i)
            if n // i != i:
                ans.add(n // i)
    return ans

def main():
    N = I()
    cnt = 0
    yakusu = get_yakusu(N)
    n1yakusu = get_yakusu(N-1)
    if N == 2:
        print(1)
        return
    for n in yakusu:
        cpN = N
        while True:
            if n == 1:
                break
            if cpN % n == 0:
                cpN = cpN // n
            else:
                break
        if cpN % n == 1:
            cnt += 1
    cnt += len(n1yakusu)
    print(cnt)
main()
# Buggy code:
N = int(input())

if N == 2:
  print(1)
  exit()

ans = 0

def factorization(n):
  lis = []
  if n % 2 == 0:
    c = 0
    while n%2 == 0:
      n //= 2
      c += 1
    lis.append([2, c])
  k = 3
  while k*k <= n:
    if n%k == 0:
      c = 0 
      while n%k == 0:
        n //= k
        c += 1
      lis.append([k, c])
    else:
      k += 2
  if n > 1:
    lis.append([n, 1])
  return lis

list1 = factorization(N-1)
ans1 = 1
for k in range(len(list1)):
  ans1 *= list1[k][1]+1
ans += ans1-1

def operation(K):
  while N%K == 0:
    N //= K
  if N%K == 1:
    return True
  else:
    return False

list2 = factorization(N)
factorlist = [1]
for l in range(len(list2)):
  list3 = []
  for j in range(list2[l][1]):
    for k in range(len(factorlist)):
      list3.append(factorlist[k]*list2[l][0]**(j+1))
  factorlist += list3
factorlist = factorlist[1:-1]
ans += 1
for item in factorlist:
  if operation(item):
    ans +=1
print(ans)

# Fixed code:
N = int(input())

if N == 2:
  print(1)
  exit()

ans = 0

def factorization(n):
  lis = []
  if n % 2 == 0:
    c = 0
    while n%2 == 0:
      n //= 2
      c += 1
    lis.append([2, c])
  k = 3
  while k*k <= n:
    if n%k == 0:
      c = 0 
      while n%k == 0:
        n //= k
        c += 1
      lis.append([k, c])
    else:
      k += 2
  if n > 1:
    lis.append([n, 1])
  return lis

list1 = factorization(N-1)
ans1 = 1
for k in range(len(list1)):
  ans1 *= list1[k][1]+1
ans += ans1-1

def operation(K):
  n = N
  while n%K == 0:
    n //= K
  if n%K == 1:
    return True
  else:
    return False

list2 = factorization(N)
factorlist = [1]
for l in range(len(list2)):
  list3 = []
  for j in range(list2[l][1]):
    for k in range(len(factorlist)):
      list3.append(factorlist[k]*list2[l][0]**(j+1))
  factorlist += list3
factorlist = factorlist[1:-1]
ans += 1
for item in factorlist:
  if operation(item):
    ans +=1
print(ans)
# Buggy code:
import math
import collections

N = int(input())
res = 1


n = 10 ** 6 + 5000
fl = [1] * n
fl[0] = 0
fl[1] = 0
lprime = []
for i in range(n):
    if fl[i] == 1:
        lprime.append(i)
        ti = i * 2
        while ti < n:
            fl[ti] = 0
            ti += i


def prime_factorize(n):
    global lprime
    a = []
    for p in lprime:
        if p * p > n:
            break
        while n % p == 0:
            a.append(p)
            n //= p
    if n != 1:
        a.append(n)
    return a


def yakusuu(N):
    t = math.floor(math.sqrt(N))
    res = []
    for i in range(2, t + 1):
        if N % i == 0:
            res.append(t)
    return res


tres = prime_factorize(N - 1)
ctres = collections.Counter(tres)
for s in ctres.values():
    res *= s + 1
tres = yakusuu(N)
if len(tres) != 0:
    for c in tres:
        t = N
        while t % c == 0:
            t //= c
        if t == 1 or (t - 1) % c == 0:
            res += 1
print(res)


# Fixed code:
import math
import collections

N = int(input())
res = 1


n = 10 ** 6 + 5000
fl = [1] * n
fl[0] = 0
fl[1] = 0
lprime = []
for i in range(n):
    if fl[i] == 1:
        lprime.append(i)
        ti = i * 2
        while ti < n:
            fl[ti] = 0
            ti += i


def prime_factorize(n):
    global lprime
    a = []
    for p in lprime:
        if p * p > n:
            break
        while n % p == 0:
            a.append(p)
            n //= p
    if n != 1:
        a.append(n)
    return a


def yakusuu(N):
    t = math.floor(math.sqrt(N))
    res = []
    for i in range(2, t + 1):
        if N % i == 0:
            res.append(i)
    return res


tres = prime_factorize(N - 1)
ctres = collections.Counter(tres)
for s in ctres.values():
    res *= s + 1
tres = yakusuu(N)
for c in tres:
    t = N
    while t % c == 0:
        t //= c
    if t == 1 or (t - 1) % c == 0:
        res += 1
print(res)
# Buggy code:
n = int(input())
a = [n]
if n != 2:
    a.append(n - 1)

def div_(N, k):
    while N % k == 0:
        N /= k
    if N % k == 1:
        a.append(k)

for k in range(2, int(n ** 0.5) + 1):
    if n % k == 0:
        div_(n,k)
        if k != n / k:
            div_(n, n // k)

    if (n - 1) % k == 0:
        a.append(k)
        if k != (n - 1) / k:
            a.append((n - 1) // k)

print(a)
print(len(a))

# Fixed code:
n = int(input())
a = [n]
if n != 2:
    a.append(n - 1)

def div_(N, k):
    while N % k == 0:
        N /= k
    if N % k == 1:
        a.append(k)

for k in range(2, int(n ** 0.5) + 1):
    if n % k == 0:
        div_(n,k)
        if k != n / k:
            div_(n, n // k)

    if (n - 1) % k == 0:
        a.append(k)
        if k != (n - 1) / k:
            a.append((n - 1) // k)

print(len(a))
# Buggy code:
K, N = map(int, input().split())
A = list(map(int, input().split()))

dist = [0] * (N-1)

for i in range(N-1):
  dist[i] = A[i+1] - A[i]
    
print(K - max(dist))

# Fixed code:
K, N = map(int, input().split())
A = list(map(int, input().split()))

dist = [0] * (N)

for i in range(N-1):
  dist[i] = A[i+1] - A[i]
dist[N-1] = A[0] - A[-1] + K

print(K - max(dist))
# Buggy code:
a,b=map(int,input().split())
s=list(map(int,input().split()))
ans=[]
for i in range(b-1):
    ans.append(s[i]-s[i-1])
ans.append(a-s[b]+s[0])
print(a-max(ans))







# Fixed code:
a,b=map(int,input().split())
s=list(map(int,input().split()))
ans=[]
for i in range(b-1):
    ans.append(s[i+1]-s[i])
ans.append(a-s[b-1]+s[0])
print(a-max(ans))
# Buggy code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
max = a[len(a) - 1] - a[0]
for i in range(len(a) - 1):
    t = a[i + 1] - a[i]
    if(t > max): max = t

print(max)

# Fixed code:
k, n = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
max = k - a[len(a) - 1] + a[0]
for i in range(len(a) - 1):
    t = a[i + 1] - a[i]
    if(t > max): max = t

print(k - max)
# Buggy code:
K, N = map(int, input().split())
A = list(map(int, input().split())) 

maxdis = 0
for i in range(len(A)-1):
  if maxdis < A[i+1] - A[i]:
    maxdis = A[i+1] - A[i]
if maxdis < A[-1] - A[0]:
  maxdis = A[-1] - A[0]
print(K-maxdis)


# Fixed code:
K, N = map(int, input().split())
A = list(map(int, input().split())) 

maxdis = 0
for i in range(len(A)-1):
  if maxdis < A[i+1] - A[i]:
    maxdis = A[i+1] - A[i]
if maxdis < (K - A[-1]) + A[0]:
  maxdis = (K - A[-1]) + A[0]
print(K-maxdis)
# Buggy code:
k ,n  = map(int, input().split(" "))
alist = list(map(int, input().split(" "))
glist =  []
for i in range(n-1):
    glist.append(alist[i+1]-alist[i])
glist.append(k+alist[0]-alist[n-1])

print(int(k-max(glist)))
    
   

# Fixed code:
k,n = map(int,input().split(" "))
alist = list(map(int, input().split(" ")))
glist = []
for i in range(n-1):
  glist.append(alist[i+1]-alist[i])
glist.append(k+alist[0]-alist[n-1])

print(k-max(glist))
# Buggy code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append(k-a[i]+a[0])
    print(dist)
    x = max(dist)
    print(k-x)
    return

if __name__=='__main__':
    Main()

# Fixed code:
import sys
from collections import deque
import numpy as np
import math
sys.setrecursionlimit(10**6)
def S(): return sys.stdin.readline().rstrip()
def SL(): return map(str,sys.stdin.readline().rstrip().split())
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    k,n = IL()
    a = list(IL())
    a.sort()
    dist = []
    for i in range(n):
        if i<n-1:
            dist.append(a[i+1]-a[i])
        else:
            dist.append(k-a[i]+a[0])
    x = max(dist)
    print(k-x)
    return

if __name__=='__main__':
    Main()
# Buggy code:
M,N = list(map(int,input().split()))
A = input().split()
A.append(M)
dis = 0
for i in range(N):
    if A[i+1]-A[i] >= dis:
        dis = A[i+1]-A[i]
        
print(A[-2]-A[0]-(dis-A[1]-A[0]))

# Fixed code:
M,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(M+A[0])
dis = 0
for i in range(N):
    if A[i+1]-A[i] >= dis:
        dis = A[i+1]-A[i]
print(M-dis)
# Buggy code:


K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
A.append(K)
B = []
ans = K
for i in range(N):
    B.append(abs(A[i]-A[i+1]))
print(B)
print(K-max(B))


# Fixed code:


K,N = list(map(int,input().split()))
A = list(map(int,input().split()))
B = []
for i in range(N-1):
    B.append(abs(A[i+1]-A[i]))
B.append(K+A[0]-A[-1])
#print(B)
print(K-max(B))
# Buggy code:
K,N = map(int,input().split())
A = list(map(int,input()))
L = []
T = K+A[0]-A[-1]
for i in range(N-1):
  L.append(A[i+1]-A[i])
print(K-max(L))

# Fixed code:
K,N = map(int,input().split())
A = list(map(int,input().split()))
L = []
T = K+A[0]-A[-1]
for i in range(N-1):
  L.append(A[i+1]-A[i])
L.append(T)
print(K-max(L))
# Buggy code:
K, N = map(int, input().split())
L = list(map(int, input().split()))
sa = [L[i+1]-L[i] for i in range(N-1)]
sa += K-L[N-1]+L[0]
ans = K-max(sa)
print(ans)

# Fixed code:
K, N = list(map(int, input().split()))
L = list(map(int, input().split()))
sa = [L[i+1]-L[i] for i in range(N-1)]
sa += [K-L[N-1]+L[0]]
ans = K-max(sa)
print(ans)
# Buggy code:
k,n = map(int,input().split())	
a = list(map(int,input().split()))
D=[A[0]-A[-1]+K]
for i in range(N-1):
    D.append(A[i+1]-A[i])
print(K-max(D))

# Fixed code:
K,N = map(int,input().split())	
A = list(map(int,input().split()))
D=[A[0]-A[-1]+K]
for i in range(N-1):
    D.append(A[i+1]-A[i])
print(K-max(D))
# Buggy code:
K,N=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(N):
    X=a_list[i]-a_list[i-1]
    b_list.append(abs(X))
print(K-max(b_list))

# Fixed code:
K,N=map(int,input().split())
a_list=list(map(int,input().split()))
b_list=[]
for i in range(N-1):
    X=a_list[i+1]-a_list[i]
    b_list.append(abs(X))
b_list.append(K-a_list[-1]+a_list[0])
print(K-max(b_list))
# Buggy code:
K,N = map(int,input().split())
A_str = input().split()
A = [int(h) for h in A_str]
print(A)
D = []
for i in range(1,N):
  D.append(A[i]-A[i-1])
D.append(A[0]+(K-A[N-1]))
print(D)
maximum = max(D)
print(K-maximum)

# Fixed code:
K,N = map(int,input().split())
A_str = input().split()
A = [int(h) for h in A_str]
D = []
for i in range(1,N):
  D.append(A[i]-A[i-1])
D.append(A[0]+(K-A[N-1]))
maximum = max(D)
print(K-maximum)
# Buggy code:
K, N = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

tmp = 0
for i in range(N-1):
  tmp = max(tmp, A[i+1]-A[i])
  
tmp = max(tmp, K - A[-1])

print(K - tmp)

# Fixed code:
K, N = map(int, input().split())
A = list(map(int, input().split()))

A.sort()

tmp = 0
for i in range(N-1):
  tmp = max(tmp, A[i+1]-A[i])
  
tmp = max(tmp, K + A[0] - A[-1])

print(K - tmp)
# Buggy code:
k, n = map(int, input().split())
a = list(map(int, input().split()))

lst = []
for i in range(0, len(a)-1) :
    lst += [a[i + 1] - a[i]]
lst += [k - a[-1] + a[0]]
print(lst)

print(sum(lst) - max(lst))


# Fixed code:
k, n = map(int, input().split())
a = list(map(int, input().split()))

lst = []
for i in range(0, len(a)-1) :
    lst += [a[i + 1] - a[i]]
lst += [k - a[-1] + a[0]]

print(sum(lst) - max(lst))
# Buggy code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
dist = [k-dist[n-1]+dist[0]]
for i in range(n):
  dist.append(a[i+1]-a[i])

print(k-max(dist))


# Fixed code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
dist = [k-a[n-1]+a[0]]
for i in range(n-1):
  dist.append(a[i+1]-a[i])

print(k-max(dist))
# Buggy code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
diff=0
for i in range(len(a)-1):
  diff_a=a[i+1]-a[i]
  if diff<diff_a:
    diff=diff_a
if a[n-1]-a[0]>diff:
  diff=a[n-1]-a[0]
print(k-diff)

# Fixed code:
k,n=map(int,input().split())
a=list(map(int,input().split()))
diff=0
for i in range(len(a)-1):
  diff_a=a[i+1]-a[i]
  if diff<diff_a:
    diff=diff_a
diff_b=(k-a[n-1])+a[0]
if diff_b>diff:
  diff=diff_b
print(k-diff)
# Buggy code:
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(min_LIST[i] <= (LIST[i + 1] - LIST[i])):
      x = (LIST[i + 1] - LIST[i])
      print(x)

  print(min_LIST)

  min_LIST.append(LIST[0] + (k - LIST[-1]))
  if(x <= (LIST[0] + (k - LIST[-1]))):
    x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()






# Fixed code:
def main():
  k, N = (int(x) for x in input().split())
  min_LIST = []
  LIST = []
  LIST = list(map(int,input().split()))
  x = 0

  for i in range(len(LIST) - 1):
    min_LIST.append(LIST[i + 1] - LIST[i])
    if(x <= min_LIST[i]):
      x = min_LIST[i]

  if(N != 2):
    min_LIST.append(LIST[0] + (k - LIST[-1]))
    if(x <= (LIST[0] + (k - LIST[-1]))):
      x = (LIST[0] + (k - LIST[-1]))

  sum_list = sum(min_LIST) - x

  print(sum_list)
if __name__ == "__main__":
  main()
# Buggy code:
K,N = map(int,input().split())
A = list(map(int,input().split()))

S =[A[0]]+ [0]*(N)

for i in range(N):
    if i==N-1:
        S[i+1] = K- A[i]
    else:
        S[i+1] = A[i+1] - A[i]

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)

# Fixed code:
K,N = map(int,input().split())
A = list(map(int,input().split()))

S = [0]*(N)

for i in range(N):
      
    if i==N-1:
        S[i] = K + A[0] -A[i]
    else:
        S[i] = A[i+1] - A[i]

Smax = S.index(max(S))

ans = sum(S)-S[Smax]
print(ans)
# Buggy code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])
new.sort()
print(sum(new[:y-1]))

# Fixed code:
K, N = map(int, input().split())
A = list(map(int, input().split()))
new = []
for i in range(N-1):
    new.append(A[i+1]-A[i])
new.append(K + A[0] - A[-1])
new.sort()
print(sum(new[:-1]))
# Buggy code:
K, N = map(int, input().split())

A = list(map(int, input().split()))
A.append(A[0])

a = [A[i + 1] - A[i] for i in range(N - 1)]
a.append(K + A[0] - A[-1])

a.sort()
print(sum(a[:-1]))


# Fixed code:
K, N = map(int, input().split())

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

a = [A[i + 1] - A[i] for i in range(N - 1)]
a.append(K + A[0] - A[-1])

a.sort()
print(sum(a[:-1]))