Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n % K]:
            continue
        visited[n % K] = True
        if n % K == 0:
            print(s)
            return
        q.appendleft((n*10, s))
        q.append((n+1, s+1))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n]:
            continue
        visited[n] = True
        if n == 0:
            print(s)
            return
        q.appendleft((n*10 % K, s))
        q.append(((n+1) % K, s+1))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n % K]:
            continue
        visited[n % K] = True
        if n % K == 0:
            print(s)
            return
        q.appendleft((n*10, s))
        q.append((n+1, s+1))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n]:
            continue
        visited[n] = True
        if n == 0:
            print(s)
            return
        q.appendleft((n*10 % K, s))
        q.append(((n+1) % K, s+1))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if n % K == 0:
            print(s)
            return
        if visited[n % K]:
            continue
        visited[n % K] = True
        if not visited[n*10 % K]:
            q.appendleft((n*10, s))
        if not visited[(n+1) % K]:
            q.append((n+1, s+1))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n]:
            continue
        visited[n] = True
        if n == 0:
            print(s)
            return
        q.appendleft((n*10 % K, s))
        q.append(((n+1) % K, s+1))


if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if n % K == 0:
            print(s)
            return
        if visited[n % K]:
            continue
        visited[n % K] = True
        q.appendleft((n*10, s))
        q.append((n+1, s+1))


if __name__ == "__main__":
    main()


Fixed submission:
from collections import deque


def main():
    K = int(input())
    q = deque([(1, 1)])
    visited = [False] * K
    while len(q):
        n, s = q.popleft()
        if visited[n]:
            continue
        visited[n] = True
        if n == 0:
            print(s)
            return
        q.appendleft((n*10 % K, s))
        q.append(((n+1) % K, s+1))


if __name__ == "__main__":
    main()