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

INF = float("inf")


def Topological_sorting(E):
    N = len(E)
    # 入次数0の節点を列挙
    in_deg = [0]*N
    for i in range(N):
        for to in E[i]:
            in_deg[to] += 1

    head = 0
    ans = [v for v in range(N) if in_deg[v] == 0]

    # 入次数0のものを順番に走査。同時に入次数を更新
    while head < len(ans):                  # O(V+E)
        v = ans[head]
        for t in E[v]:
            in_deg[t] -= 1
            if in_deg[t] == 0:
                ans.append(t)
        head += 1
    if len(ans) != N:
        return False
    return ans


def main():
    N, M = map(int, input().split())
    E = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].append(b-1)

    # print(rE)
    loop = Topological_sorting(E)
    if loop is not False:
        print(-1)
        return

    need = [1]*N
    while True:
        # 閉路を一つ探す。
        # グラフの単純化から始める
        # 入次数0の子はいらない子。
        # 出次数0の子はいらない子。

        in_deg = [0]*N
        out_deg = [0]*N
        for i in range(N):
            out_deg[i] = len(E[i])
            for to in E[i]:
                in_deg[to] += 1
        for i in range(N):
            if in_deg[i] == 0 or out_deg[i] == 0:
                need[i] = 0
        for i in range(N):
            if need[i] == 0:
                E[i] = []
            else:
                E[i] = [u for u in E[i] if need[u] == 1]

        # 答えがすでに得られているなら、
        # breakする。
        in_deg = [0]*N
        for i in range(N):
            for to in E[i]:
                in_deg[to] += 1
        cand = [i for i in range(N) if in_deg[i] > 1]
        # print("cand", cand, need)
        # print(E)
        if len(cand) == 0:
            break

        # 用いられない辺が存在するとのこと。
        # その終点からスタートする
        # 簡単に、入次数が1以上の点だ
        start = cand[0]
        q = [start]
        hist = set()
        while q:
            v = q[-1]
            hist.add(v)
            q.extend(E[v])
            if v == q[-1]:
                q.pop()
                hist.remove(v)
            loop = False
            for u in E[v]:
                if u in hist:
                    loop = True
                    break
            if loop:
                break
        # print("hist", hist)
        for i in range(N):
            if i not in hist:
                need[i] = 0
        # print(need)

    ans = [i for i, n in enumerate(need, start=1) if n == 1]
    print(len(ans))
    print(*ans, sep="\n")
    return


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
# https://atcoder.jp/contests/abc142/submissions/7764934
# ほぼまんま。


def main():
    N, M = map(int, input().split())
    E = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].add(b-1)

    # スタート地点は全て試す
    for start in range(N):
        visited = [0]*N
        Q = deque([(start, {start})])
        append, popleft = Q.append, Q.popleft
        visited[start] = 1

        while Q:
            v, route = popleft()
            # これまで通ってきた頂点と行き先の積集合
            s = route & E[v]
            # sが空集合(ループでない)か、行き先に始点のみが含まれる場合は継続
            # 行き先に始点以外の場所が含まれるなら、もっと良い始点の選び方がある。
            # if not (not s or (s == {start})):
            if s and s != {start}:
                continue
            for dest in E[v]:
                if dest == start:
                    print(len(route))
                    print(*[n+1 for n in route], sep="\n")
                    return
                if visited[dest]:
                    continue
                visited[dest] = 1
                append((dest, route | {dest}))
    print(-1)
    return


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
INF = float("inf")


def Topological_sorting(E):
    N = len(E)
    # 入次数0の節点を列挙
    in_deg = [0]*N
    for i in range(N):
        for to in E[i]:
            in_deg[to] += 1

    head = 0
    ans = [v for v in range(N) if in_deg[v] == 0]

    # 入次数0のものを順番に走査。同時に入次数を更新
    while head < len(ans):                  # O(V+E)
        v = ans[head]
        for t in E[v]:
            in_deg[t] -= 1
            if in_deg[t] == 0:
                ans.append(t)
        head += 1
    if len(ans) != N:
        return False, ans
    return True, ans


def reversed_graph(E):
    rev = [[] for _ in E]
    for i, dest in enumerate(E):
        for t in dest:
            rev[t].append(i)
    return rev


def main():
    N, M = map(int, input().split())
    E = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].append(b-1)

    rE = reversed_graph(E)
    # print(rE)

    looped1, order1 = Topological_sorting(E)
    looped2, order2 = Topological_sorting(rE)
    # print(looped1, order1)
    # print(looped2, order2)

    if looped1 and looped2:
        print(-1)
        return

    nodes = set(range(N))-set(order1)-set(order2)
    # nodes内部でlevel-order-likeな探索が必要
    q = [list(nodes)[0]]
    seen = [0]*N
    prev = [-1]*N
    head = 0
    while head < len(q):
        v = q[head]
        seen[v] = 1
        poi = set()
        for u in E[v]:
            if u not in nodes:
                continue
            if seen[u] == 1:
                # ループ
                poi.add(u)
                prev[u] = v
                continue
            q.append(u)
            prev[u] = v
        if poi:
            # print(poi, v)
            ans = []
            while v not in poi:
                ans.append(v)
                v = prev[v]
            ans.append(v)
            break

        head += 1
    print(len(ans))
    print(*[a+1 for a in ans], sep="\n")


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
# https://atcoder.jp/contests/abc142/submissions/7764934
# ほぼまんま。


def main():
    N, M = map(int, input().split())
    E = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].add(b-1)

    # スタート地点は全て試す
    for start in range(N):
        visited = [0]*N
        Q = deque([(start, {start})])
        append, popleft = Q.append, Q.popleft
        visited[start] = 1

        while Q:
            v, route = popleft()
            # これまで通ってきた頂点と行き先の積集合
            s = route & E[v]
            # sが空集合(ループでない)か、行き先に始点のみが含まれる場合は継続
            # 行き先に始点以外の場所が含まれるなら、もっと良い始点の選び方がある。
            # if not (not s or (s == {start})):
            if s and s != {start}:
                continue
            for dest in E[v]:
                if dest == start:
                    print(len(route))
                    print(*[n+1 for n in route], sep="\n")
                    return
                if visited[dest]:
                    continue
                visited[dest] = 1
                append((dest, route | {dest}))
    print(-1)
    return


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
INF = float("inf")


def Topological_sorting(E):
    N = len(E)
    in_deg = [0]*N
    for i in range(N):        # ２重ループでO(E)
        for to_ in E[i]:
            in_deg[to_] += 1

    curr = 0
    ans = [v for v in range(N) if in_deg[v] == 0]

    while curr < len(ans):                  # O(V+E)
        v = ans[curr]
        for t in E[v]:
            in_deg[t] -= 1
            if in_deg[t] == 0:
                ans.append(t)
        curr += 1
    if len(ans) != N:
        return False, ans
    return True, ans


def main():
    N, M = map(int, input().split())
    E = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].append(b-1)

    # 入次数、出次数をカウントして、
    # 誘導部分グラフにならない場所を削っていく
    not_looped, ans = Topological_sorting(E)
    if not_looped:
        print(-1)               # DAGなら誘導部分グラフは存在していない
        return
    # 存在するなら、単に閉路を見つければ良い
    in_deg = [0]*N
    for i in range(N):
        for to in E[i]:
            in_deg[to] += 1

    # 深さ優先探索をする
    curr = 0
    cand = []
    while not cand:
        cand = [v for v in range(N) if in_deg[v] == curr]
        curr += 1
    for v in cand:
        prev = [-1]*N
        seen = [-1]*N
        seen[v] = 0
        q = [v]
        head = 0
        while q:
            node = q[head]
            head += 1
            seen[node] = 1
            for u in E[node]:
                if seen[u] == 1:
                    # ループだ
                    prev[u] = node
                    w = node
                    ans.append(node+1)
                    while node != prev[w]:
                        ans.append(prev[w]+1)
                        w = prev[w]
                    print(len(ans))
                    print("\n".join(map(str, ans)))
                    return
                if seen[u] == 0:
                    continue
                q.append(u)
                prev[u] = node
                seen[u] = 0
    print(-1)


if __name__ == '__main__':
    main()


Fixed submission:
import sys
from collections import deque
# https://atcoder.jp/contests/abc142/submissions/7764934
# ほぼまんま。


def main():
    N, M = map(int, input().split())
    E = [set() for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        E[a-1].add(b-1)

    # スタート地点は全て試す
    for start in range(N):
        visited = [0]*N
        Q = deque([(start, {start})])
        append, popleft = Q.append, Q.popleft
        visited[start] = 1

        while Q:
            v, route = popleft()
            # これまで通ってきた頂点と行き先の積集合
            s = route & E[v]
            # sが空集合(ループでない)か、行き先に始点のみが含まれる場合は継続
            # 行き先に始点以外の場所が含まれるなら、もっと良い始点の選び方がある。
            # if not (not s or (s == {start})):
            if s and s != {start}:
                continue
            for dest in E[v]:
                if dest == start:
                    print(len(route))
                    print(*[n+1 for n in route], sep="\n")
                    return
                if visited[dest]:
                    continue
                visited[dest] = 1
                append((dest, route | {dest}))
    print(-1)
    return


if __name__ == '__main__':
    main()