Buggy python submission (verdict: Time Limit Exceeded):
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif k == 2:
    if n % 2 == 0:
      print(2)
    else:
      print(3)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Fixed submission:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif n <= abs(n-k):
    print(n)
  else:
    ans = n % k
    if ans > 2:
      while 1:
        hi = abs(ans-k)
        if hi < zen:
          zen = hi
          ans = hi

        else:
          print(ans)
          exit()
    else:
      print(ans)
    
      
if __name__ == '__main__':
  main()

Buggy python submission (verdict: Runtime Error):
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif k == 2:
    if n % 2 = 0:
      print(2)
    else:
      print(3)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Fixed submission:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif n <= abs(n-k):
    print(n)
  else:
    ans = n % k
    if ans > 2:
      while 1:
        hi = abs(ans-k)
        if hi < zen:
          zen = hi
          ans = hi

        else:
          print(ans)
          exit()
    else:
      print(ans)
    
      
if __name__ == '__main__':
  main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  else:  
    while 1:
      hi = abs(ans-k)
      if hi < zen:
        zen = hi
        ans = hi
        
      else:
        print(ans)
        exit()
      
if __name__ == '__main__':
  main()

Fixed submission:
def main():
  #n = int(input())
  #s = input()
  #s = input().split()
  n,k = list(map(int,input().split()))
  #a = list(map(int,input().split()))
  #a = [input() for i in range(n)]
  #a = [int(input()) for i in range(n)]
  #a = [input().split() for i in range(n)]
  #a = [list(map(int,input().split())) for i in range(n)]
  ans = n
  zen = n
  count = 0
  lis = []
  
  if k == 1:
    print(0)
  elif n <= abs(n-k):
    print(n)
  else:
    ans = n % k
    if ans > 2:
      while 1:
        hi = abs(ans-k)
        if hi < zen:
          zen = hi
          ans = hi

        else:
          print(ans)
          exit()
    else:
      print(ans)
    
      
if __name__ == '__main__':
  main()