Buggy python submission (verdict: Time Limit Exceeded):
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)

Fixed submission:
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)

Buggy python submission (verdict: Time Limit Exceeded):
# あっとこ
# ぴゅあ
from collections import deque
import sys
import heapq
sys.setrecursionlimit(10**7)


def input(): return sys.stdin.readline().rstrip()

def negative_circle(s, G, n):
    dist = [10**20 for i in range(n+1)]
    dist[s] = 0
    for i in range(1, n+1):
        flag = False
        for some in G:
            a, b = some[0], some[1]
            newlen = dist[a] - 1
            if newlen < dist[b]:
                flag = True
                dist[b] = newlen

        if not flag:
            break
        if i == n:
            return True
    return False


N, M = map(int, input().split())
G = []
graph = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
    graph[a].append(b)

have_circle = negative_circle(1, G, N)


if not have_circle:
    print(-1)
    sys.exit()

visited = [False for i in range(N + 1)]
parent = [-1 for i in range(N+1)]
circle_part = []


def dfs(node):
    if visited[node]==True: pass
    visited[node] = True
    for child in graph[node]:
        if visited[child] == True:
            parent[child] = node
            circle_part.append(child)
            return
        else:
            parent[child] = node
            dfs(child)


for i in range(1,N+1):
    if visited[i] == False:
        dfs(i)

now_node = circle_part[0]
A = deque([])

now_node = circle_part[0]
A = deque([now_node])
while parent[now_node] != A[0]:
    now_node = parent[now_node]
    A.append(now_node)
A.reverse()
A.appendleft(A.pop())
def make_graph(G, A):
    A_set = set(A)
    graph_A = {A_node: [] for A_node in A}
    edge_A = []
    for x, y in G:
        if x in A_set and y in A_set:
            graph_A[x].append(y)
            edge_A.append((x, y))
    return graph_A, edge_A


graph_A, edge_A = make_graph(G, A)

while len(edge_A) != len(graph_A):
    # 条件を満たさない間
    P = set([(A[i-1], A[i]) for i in range(len(A))])
    S, E = 0, 0
    for pair in edge_A:
        if pair not in P:
            S, E = pair
            break
    visited = [False for i in range(N + 1)]
    circle_part = []
    dfs(E)
    A.clear()
    A = deque([S, E])
    now_node = S
    while parent[now_node] != E:
        now_node = parent[now_node]
        A.append(now_node)
    A.reverse()
    A.appendleft(A.pop())
    graph_A, edge_A = make_graph(G, A)

print(len(A))
for x in A:
    print(x)





Fixed submission:
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)

Buggy python submission (verdict: Runtime Error):
# あっとこ
# ぴゅあ
from collections import deque
import sys
import heapq
sys.setrecursionlimit(10**7)


def input(): return sys.stdin.readline().rstrip()

K=0
def negative_circle(s, G, n):
    dist = [10**20 for i in range(n+1)]
    dist[s] = 0
    for i in range(1, n+1):
        flag = False
        for some in G:
            a, b = some[0], some[1]
            newlen = dist[a] - 1
            if newlen < dist[b]:
                flag = True
                dist[b] = newlen

        if not flag:
            break
        if i == n:
            return True
    return False


N, M = map(int, input().split())
G = []
graph = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
    graph[a].append(b)

have_circle = negative_circle(1, G, N)


if not have_circle:
    print(-1)
    sys.exit()

visited = [False for i in range(N + 1)]
parent = [-1 for i in range(N+1)]
circle_part = []


def dfs(node):
    visited[node] = True
    for child in graph[node]:
        if visited[child] == True:
            parent[child] = node
            circle_part.append(child)
            return
        else:
            if not circle_part:
                parent[child] = node
                dfs(child)


dfs(G[0][0])
now_node = circle_part[0]
A = deque([now_node])
while parent[now_node] != A[0]:
    now_node = parent[now_node]
    A.append(now_node)
A.reverse()
A.appendleft(A.pop())


def make_graph(G, A):
    A_set = set(A)
    graph_A = {A_node: [] for A_node in A}
    edge_A = []
    for x, y in G:
        if x in A_set and y in A_set:
            graph_A[x].append(y)
            edge_A.append((x, y))
    return graph_A, edge_A


graph_A, edge_A = make_graph(G, A)

while len(edge_A) != len(graph_A):
    # 条件を満たさない間
    P = set([(A[i-1], A[i]) for i in range(len(A))])
    S, E = 0, 0
    for pair in edge_A:
        if pair not in P:
            S, E = pair
            break
    visited = [False for i in range(N + 1)]
    circle_part = []
    dfs(E)
    A.clear()
    A = deque([S, E])
    now_node = S
    while parent[now_node] != E:
        now_node = parent[now_node]
        A.append(now_node)
    A.reverse()
    A.appendleft(A.pop())
    graph_A, edge_A = make_graph(G, A)

print(len(A))
for x in A:
    print(x)

Fixed submission:
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)

Buggy python submission (verdict: Runtime Error):
# あっとこ
# ぴゅあ
from collections import deque
import sys
import heapq
sys.setrecursionlimit(10**7)


def input(): return sys.stdin.readline().rstrip()


def negative_circle(s, G, n):
    dist = [10**20 for i in range(n+1)]
    dist[s] = 0
    for i in range(1, n+1):
        flag = False
        for some in G:
            a, b = some[0], some[1]
            newlen = dist[a] - 1
            if newlen < dist[b]:
                flag = True
                dist[b] = newlen
        if not flag:
            break
        if i == n:
            return True
    return False


N, M = map(int, input().split())
G = []
graph = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
    graph[a].append(b)

have_circle = negative_circle(1, G, N)


if not have_circle:
    print(-1)
    sys.exit()

visited = [False for i in range(N + 1)]
parent = [-1 for i in range(N+1)]
circle_part = []


def dfs(node):
    visited[node] = True
    for child in graph[node]:
        if visited[child] == True:
            parent[child] = node
            circle_part.append(child)
            return
        else:
            parent[child] = node
            dfs(child)


dfs(1)
now_node = circle_part[0]
A = deque([now_node])
while parent[now_node] != A[0]:
    now_node = parent[now_node]
    A.append(now_node)
A.reverse()
A.appendleft(A.pop())


def make_graph(G, A):
    A_set = set(A)
    graph_A = {A_node: [] for A_node in A}
    edge_A = []
    for x, y in G:
        if x in A_set and y in A_set:
            graph_A[x].append(y)
            edge_A.append((x, y))
    return graph_A, edge_A


graph_A, edge_A = make_graph(G, A)

while len(edge_A) != len(graph_A):
    # 条件を満たさない間
    P = set([(A[i-1], A[i]) for i in range(len(A))])
    S, E = 0, 0
    for pair in edge_A:
        if pair not in P:
            S, E = pair
            break
    visited = [False for i in range(N + 1)]
    parent = [-1 for i in range(N+1)]
    circle_part = []
    dfs(E)
    A.clear()
    A = deque([S, E])
    now_node = S
    while parent[now_node] != E:
        now_node = parent[now_node]
        A.append(now_node)
    A.reverse()
    A.appendleft(A.pop())
    graph_A.clear()
    edge_A.clear()
    graph_A, edge_A = make_graph(G, A)

print(len(A))
for x in A:
    print(x)

Fixed submission:
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)

Buggy python submission (verdict: Runtime Error):
# あっとこ
# ぴゅあ
from collections import deque
import sys
import heapq
sys.setrecursionlimit(10**7)


def input(): return sys.stdin.readline().rstrip()


def negative_circle(s, G, n):
    dist = [10**20 for i in range(n+1)]
    dist[s] = 0
    for i in range(1, n+1):
        flag = False
        for some in G:
            a, b = some[0], some[1]
            newlen = dist[a] - 1
            if newlen < dist[b]:
                flag = True
                dist[b] = newlen
        if not flag:
            break
        if i == n:
            return True
    return False


N, M = map(int, input().split())
G = []
graph = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
    graph[a].append(b)

have_circle = negative_circle(1, G, N)


if not have_circle:
    print(-1)
    sys.exit()

visited = [False for i in range(N + 1)]
parent = [-1 for i in range(N+1)]
circle_part = []


def dfs(node):
    visited[node] = True
    for child in graph[node]:
        if visited[child] == True:
            parent[child] = node
            circle_part.append(child)
            return
        else:
            parent[child] = node
            dfs(child)


dfs(1)
now_node = circle_part[0]
A = deque([now_node])
while parent[now_node] != A[0]:
    now_node = parent[now_node]
    A.append(now_node)
A.reverse()
A.appendleft(A.pop())


def make_graph(G, A):
    A_set = set(A)
    graph_A = {A_node: [] for A_node in A}
    edge_A = []
    for x, y in G:
        if x in A_set and y in A_set:
            graph_A[x].append(y)
            edge_A.append((x, y))
    return graph_A, edge_A


graph_A, edge_A = make_graph(G, A)

while len(edge_A) != len(graph_A):
    # 条件を満たさない間
    P = set([(A[i-1], A[i]) for i in range(len(A))])
    S, E = 0, 0
    for pair in edge_A:
        if pair not in P:
            S, E = pair
            break
    visited = [False for i in range(N + 1)]
    parent = [-1 for i in range(N+1)]
    circle_part = []
    dfs(E)
    A.clear()
    A = deque([S, E])
    now_node = S
    while parent[now_node] != E:
        now_node = parent[now_node]
        A.append(now_node)
    A.reverse()
    A.appendleft(A.pop())
    graph_A.clear()
    edge_A.clear()
    graph_A, edge_A = make_graph(G, A)

print(len(A))
for x in A:
    print(x)

Fixed submission:
import heapq


class Edge():

    def __init__(self, _to, _cost):
        self.to = _to
        self.cost = _cost


class Dijkstra():
    def __init__(self, V):

        self.G = [[] for i in range(V+1)]
        self._E = 0
        self._V = V

    @property
    def E(self):
        """ 辺数 """
        return self._E

    @property
    def V(self):
        """ 頂点数 """
        return self._V

    def add(self, _from, _to, _cost):
        self.G[_from].append(Edge(_to, _cost))
        self._E += 1

    def shortest_path(self, s):

        que = []
        heapq.heapify(que)
        d = [10**10] * (self._V+1)
        par = [-1] * (self._V + 1)
        d[s] = 0
        heapq.heappush(que, (0, s))

        while que:
            cost, v = heapq.heappop(que)
            if d[v] < cost:
                continue

            for e in self.G[v]:
                if d[e.to] > d[v] + e.cost:
                    d[e.to] = d[v] + e.cost
                    par[e.to] = v
                    heapq.heappush(que, (d[e.to], e.to))
        return d, par


N, M = map(int, input().split())
G = []
ans = []
now_dis = 10**6
for i in range(M):
    a, b = map(int, input().split())
    G.append((a, b))
for i in range(M):
    graph = Dijkstra(N)
    for j in range(M):
        if j != i:
            graph.add(G[j][0], G[j][1], 1)
    S, T = G[i][0], G[i][1]
    d, par = graph.shortest_path(T)
    if d[S]+1 < now_dis:
        now_dis = d[S]+1
        sub = []
        node = S
        while node != T:
            sub.append(node)
            node = par[node]
        sub.append(T)
        ans = sub

K=len(ans)
if K==0:
    print(-1)
    exit()
print(K)
for x in ans:
    print(x)