Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    loop.reverse()
    t = loop[-1]
    loop = loop[loop.index(t):]
    ll = set(loop)
    flag = True
    while flag:
        for i, v in enumerate(loop[:-1]):
            for j in l[v]:
                if j in ll and loop[i + 1] != j:
                    print(i, v,j)
                    if loop.index(j) == 0:
                        loop = loop[:i+1]
                    else:
                        loop = loop[:i+1] + loop[loop.index(j):]
                    print(loop)
                    ll = set(loop)
                    break
        else:
            flag = False
    loop = set(loop)
    print(len(loop))
    for i in loop:
        print(i+1)
    return
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Runtime Error):
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
import copy
def myhash(v):
    r = 0
    for i in v:
        r += pow(2, i)
    return r
def myhash2(r, N):
    vv = set()
    for i, v in enumerate(format(r, '0'+str(N)+'b')):
        if v == '1':
            vv.add(N-i-1)
    return vv
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    a = []
    def cyclic(n, l, r):
        ans = []
        r += pow(2, n)
        v = myhash2(r, N)
        for i in  l[n]:
            if i in v:
                a.append(r)
                return
                #return True, v
        for i in l[n]:
            cyclic(i, l, r)
        return
    for i in range(N):
        r = 0
        cyclic(i, l, r)
    if a:
        m = 100000
        rr = None
        for i in a:
            x = len(myhash2(i, N))
            if m > x:
                m = x
                rr = i
        print(m)
        for i in myhash2(rr, N):
            print(i+1)
    else:
        print(-1)
    return -1
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
import copy
def myhash(v):
    r = 0
    for i in v:
        r += pow(2, i)
    return r
def myhash2(r, N):
    vv = set()
    for i, v in enumerate(format(r, '0'+str(N)+'b')):
        if v == '1':
            vv.add(N-i-1)
    return vv
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    a = set()
    def cyclic(n, l, r):
        ans = []
        r += pow(2, n)
        v = myhash2(r, N)
        for i in  l[n]:
            if i in v:
                a.add(r)
                return
                #return True, v
        for i in l[n]:
            cyclic(i, l, r)
        return
    for i in range(N):
        r = 0
        cyclic(i, l, r)
    if a:
        m = 100000
        rr = None
        for i in a:
            x = len(myhash2(i, N))
            if m > x:
                m = x
                rr = i
        print(m)
        for i in myhash2(rr, N):
            print(i+1)
    else:
        print(-1)
    return -1
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**7)
import copy
def myhash(v):
    r = 0
    for i in v:
        r += pow(2, i)
    return r
def myhash2(r, N):
    vv = set()
    for i, v in enumerate(format(r, '0'+str(N)+'b')):
        if v == '1':
            vv.add(N-i-1)
    return vv
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    a = set()
    def cyclic(n, l, r):
        ans = []
        r += pow(2, n)
        v = myhash2(r, N)
        for i in  l[n]:
            if i in v:
                a.add(r)
                return
                #return True, v
        for i in l[n]:
            cyclic(i, l, r)
        return
    for i in range(N):
        r = 0
        cyclic(i, l, r)
    if a:
        m = 100000
        rr = None
        for i in a:
            x = len(myhash2(i, N))
            if m > x:
                m = x
                rr = i
        print(m)
        for i in myhash2(rr, N):
            print(i+1)
    else:
        print(-1)
    return -1
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
import copy
import numpy as np
def cyclic(n, l, v):
    ans = []
    v.add(n)
    for i in  l[n]:
        if i in v:
            return True, v
    for i in l[n]:
        v2 = copy.deepcopy(v)
        t, vv = cyclic(i, l, v2)
        #v.remove(i)
        if t:
            ans.append(vv)
        if ans:
            return True, ans
    return False, v
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    for i in range(N):
        v = set()
        t, vv = cyclic(i, l, v)
        if t:
            r.extend(np.ndarray.flatten(np.array(vv)).tolist())
    if r:
        m = 100000
        rr = None
        for i in r:
            if m > len(i):
                m = len(i)
                rr = i
        print(m)
        for i in rr:
            print(i+1)
    else:
        print(-1)
    return -1
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()

Buggy python submission (verdict: Runtime Error):
import copy
import numpy as np
def cyclic(n, l, v):
    ans = []
    v.add(n)
    for i in  l[n]:
        if i in v:
            return True, v
    for i in l[n]:
        v2 = copy.deepcopy(v)
        t, vv = cyclic(i, l, v2)
        #v.remove(i)
        if t:
            ans.append(vv)
        if ans:
            return True, ans
    return False, v
def main():
    N, M  = map(int, input().split())
    l = [[] for _ in range(N)]
    r = []
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    for i in range(N):
        v = set()
        t, vv = cyclic(i, l, v)
        if t:
            r.extend(np.ndarray.flatten(np.array(vv)).tolist())
    if r:
        m = 100000
        rr = None
        for i in r:
            if m > len(i):
                m = len(i)
                rr = i
        print(m)
        for i in rr:
            print(i+1)
    print(-1)
    return -1
main()


Fixed submission:
import sys
sys.setrecursionlimit(10**7)
def main():
    N, M = map(int, input().split())
    l = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, input().split())
        l[a-1].append(b-1)
    c = [0] * N
    loop = []
    def cyclic(n):
        if c[n] == 2:
            return False
        if c[n] == 1:
            loop.append(n)
            return True
        c[n] = 1
        for i in l[n]:
            if cyclic(i):
                loop.append(n)
                return True
        c[n] = 2
        return False
    for i in range(N):
        if cyclic(i):
            break
    if len(loop) == 0:
        print(-1)
        return
    print(0)
    return
main()