Problem p03231 (python)

Fixed submission:
import collections
import fractions
N, M = map(int, input().split())
S = input()
T = input()
lcm = (N*M) // fractions.gcd(N, M)
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
else:
    tmp = lcm // N
    tmp2 = lcm // M
    tmp_s = collections.defaultdict(str)
    for i in range(N):
        tmp_s[tmp*i] = S[i]

    for i in range(M):
        if tmp2*i in tmp_s:
            if T[i] != tmp_s[tmp2*i]:
                print(-1)
                exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
lcm = (N*M) // fractions.gcd(N, M)
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
elif max(N, M) % min(N, M) == 0:
    if M > N:
        N, M = M, N
        S, T = T, S
    k = N // M
    for i in range(M):
        if S[i*k] != T[i]:
            print(-1)
            exit(0)
    print(lcm)
else:
    tmp = lcm // N
    tmp2 = lcm // M
    tmp_s = [""] * lcm
    for i in range(N):
        tmp_s[tmp * i] = S[i]
    for j in range(M):
        if tmp_s[tmp2 * j] != "" and tmp_s[tmp2 * j] != T[j]:
            print(-1)
            exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
else:
    lcm = (N*M) // fractions.gcd(N, M)
    tmp = lcm // N
    tmp2 = lcm // M
    tmp_s = [""] * lcm
    for i in range(N):
        tmp_s[tmp * i] = S[i]
    for j in range(M):
        if tmp_s[tmp2 * j] != "" and tmp_s[tmp2 * j] != T[j]:
            print(-1)
            exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
else:
    lcm = (N*M) // fractions.gcd(N, M)
    tmp = lcm // N
    tmp2 = lcm // M
    tmp_s = [""] * lcm
    for i in range(N):
        tmp_s[tmp * i] = S[i]
    for i in range(M):
        if tmp_s[tmp2 * i] != "" and tmp_s[tmp2 * i] != T[i]:
            print(-1)
            exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
else:
    lcm = N*M // fractions.gcd(N, M)
    count = 1
    tmp = lcm // N
    tmp2 = lcm // M
    flag = False

    tmp_s = [""] * (lcm)
    for i in range(N):
        tmp_s[tmp * i] = S[i]
    for i in range(M):
        if tmp_s[tmp2 * i] != "" and tmp_s[tmp2 * i] != T[i]:
            count += 1
            print(-1)
            exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    lcm = N*M // fractions.gcd(N, M)
    count = 1
    tmp = lcm // N
    tmp2 = lcm // M
    flag = False

    tmp_s = [""] * (lcm * count)
    for i in range(N):
        tmp_s[((lcm*count) // N) * i] = S[i]
    for i in range(M):
        if tmp_s[((lcm*count) // M) * i] != "" and tmp_s[((lcm*count) // M) * i] != T[i]:
            count += 1
            print(-1)
            exit(0)
    print(lcm)

Buggy submission (verdict: Runtime Error):
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    lcm = N*M // fractions.gcd(N, M)
    count = 1
    tmp = lcm // N
    tmp2 = lcm // M
    flag = False
    for _ in range(min(N, M)):
        tmp_s = [""] * (lcm * count)
        for i in range(N):
            tmp_s[((lcm*count) // N) * i] = S[i]
        for i in range(M):
            if tmp_s[((lcm*count) // M) * i] != "" and tmp_s[((lcm*count) // M) * i] != T[i]:
                count += 1
                flag = True
                break
        if flag:
            flag = False
            continue
        print(count * lcm)
        exit(0)
    print(-1)