diff --git a/buggy.py b/buggy.py
--- a/buggy.py
+++ b/buggy.py
@@ -18,7 +18,7 @@
     Q = collections.deque()
     Q.append((0, N - 1, 0))
     while Q:
-        taka, aoki, cost = Q.popleft()
+        taka, aoki, cost = Q.pop()
         for dst_t in G[taka]:
             for dst_a in G[aoki]:
                 if D[dst_t][dst_a] == inf and C[dst_t] != C[dst_a]:
