Buggy python submission (verdict: Runtime Error):
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M

for i in range(Sp*lcm):
    if Sp*i > lcm:
        break
    if i > N-1:
        break
    ansS[Sp*i] = S[i]

for i in range(Tp*lcm):
    if Tp*i > lcm:
        break
    if i > M-1:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break

print(ans)


Fixed submission:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
Sp = lcm//N
Tp = lcm//M
lcmlcm = (Sp * Tp) // math.gcd(Sp, Tp)
owari = min(Sp*N, M*Tp)
for i in range(0, owari, lcmlcm):
    if S[i//Sp] != T[i//Tp]:
        ans = -1
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M

for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i > N-1:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i > M-1:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break

print(ans)

Fixed submission:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
Sp = lcm//N
Tp = lcm//M
lcmlcm = (Sp * Tp) // math.gcd(Sp, Tp)
owari = min(Sp*N, M*Tp)
for i in range(0, owari, lcmlcm):
    if S[i//Sp] != T[i//Tp]:
        ans = -1
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())

lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M


for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i > N:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i > M:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break
print(ans)


Fixed submission:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
Sp = lcm//N
Tp = lcm//M
lcmlcm = (Sp * Tp) // math.gcd(Sp, Tp)
owari = min(Sp*N, M*Tp)
for i in range(0, owari, lcmlcm):
    if S[i//Sp] != T[i//Tp]:
        ans = -1
        break
print(ans)

Buggy python submission (verdict: Runtime Error):
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
ansS = ["0"]*lcm
ansT = ["0"]*lcm
Sp = lcm//N
Tp = lcm//M
i = 1
for i in range(10000000000):
    if Sp*i > lcm:
        break
    if i >= N:
        break
    ansS[Sp*i] = S[i]

for i in range(10000000000):
    if Tp*i > lcm:
        break
    if i >= M:
        break
    ansT[Tp*i] = T[i]

for i in range(lcm):
    if ansS[i] == "0" or ansT[i] == "0":
        continue
    if ansS[i] != ansT[i]:
        ans = -1
        break
print(ans)


Fixed submission:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
Sp = lcm//N
Tp = lcm//M
lcmlcm = (Sp * Tp) // math.gcd(Sp, Tp)
owari = min(Sp*N, M*Tp)
for i in range(0, owari, lcmlcm):
    if S[i//Sp] != T[i//Tp]:
        ans = -1
        break
print(ans)