Buggy python submission (verdict: Runtime Error):
def solution(X, N, P):
    if N <= 0 or X not in P:
        return X

    P.sort()
    diff = [abs(p - X) for p in P]
    X_idx = diff.index(0)
    left = sorted(diff[:X_idx])
    right = sorted(diff[X_idx+1:])

    for i in range(1, len(P)):
        left = X - i
        right = X + i

        nearest_l = None
        nearest_r = None
        if left not in P:
            nearest_l = left
            break
        if right not in P:
            nearest_r = right
            break
    else:
        nearest_r = len(P) + 1
    if nearest_l and nearest_r:
        nearest = min(nearest_l, nearest_r)
    elif nearest_l is not None:
        nearest = nearest_l
    elif nearest_r is not None:
        nearest = nearest_r
    return nearest

X, N = map(int, input().split())
P = list(map(int, input().split()))

print(solution(X, N, P))


Fixed submission:
def solution(X, N, P):
    if N <= 0 or X not in P:
        return X

    P.sort()
    diff = [abs(p - X) for p in P]
    X_idx = diff.index(0)
    left = sorted(diff[:X_idx])
    right = sorted(diff[X_idx+1:])

    for i in range(1, len(P) + 1):
        left = X - i
        right = X + i

        nearest_l = None
        nearest_r = None
        if left not in P:
            nearest_l = left
            break
        if right not in P:
            nearest_r = right
            break
    else:
        nearest_r = len(P) + 1
    if nearest_l and nearest_r:
        nearest = min(nearest_l, nearest_r)
    elif nearest_l is not None:
        nearest = nearest_l
    elif nearest_r is not None:
        nearest = nearest_r
    return nearest

X, N = map(int, input().split())
P = list(map(int, input().split()))

print(solution(X, N, P))

Buggy python submission (verdict: Runtime Error):
def solution(X, N, P):
    if N <= 0 or X not in P:
        return X

    P.sort()
    diff = [abs(p - X) for p in P]
    X_idx = diff.index(0)
    left = sorted(diff[:X_idx])
    right = sorted(diff[X_idx+1:])

    for i in range(1, len(P)):
        left = X - i
        right = X + i

        nearest_l = None
        nearest_r = None
        if left not in P and left >= 1:
            nearest_l = left
            break
        if right not in P:
            nearest_r = right
            break
    else:
        nearest_r = len(P) + 1
    if nearest_l and nearest_r:
        nearest = min(nearest_l, nearest_r)
    else:
        nearest = nearest_l or nearest_r
    return nearest

X, N = map(int, input().split())
P = list(map(int, input().split()))
print(solution(X, N, P))


Fixed submission:
def solution(X, N, P):
    if N <= 0 or X not in P:
        return X

    P.sort()
    diff = [abs(p - X) for p in P]
    X_idx = diff.index(0)
    left = sorted(diff[:X_idx])
    right = sorted(diff[X_idx+1:])

    for i in range(1, len(P) + 1):
        left = X - i
        right = X + i

        nearest_l = None
        nearest_r = None
        if left not in P:
            nearest_l = left
            break
        if right not in P:
            nearest_r = right
            break
    else:
        nearest_r = len(P) + 1
    if nearest_l and nearest_r:
        nearest = min(nearest_l, nearest_r)
    elif nearest_l is not None:
        nearest = nearest_l
    elif nearest_r is not None:
        nearest = nearest_r
    return nearest

X, N = map(int, input().split())
P = list(map(int, input().split()))

print(solution(X, N, P))