Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)        

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A+1]==Tlist[i*B+1]:
        print(-1)
        break
        
print(L)

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)

Buggy python submission (verdict: Runtime Error):
import math

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/math.gcd(N,M))
G=math.gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A+1]==Tlist[i*B+1]:
        print(-1)
        break
        
print(L)

Fixed submission:
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

N, M = map(int,input().split())
S=input()
T=input()

Slist=list(S)
Tlist=list(T)

L=int(N*M/gcd(N,M))
G=gcd(N,M)

A=int(N/G)
B=int(M/G)

for i in range(G):
    if Slist[i*A]!=Tlist[i*B]:
        ans=-1
        break
    else:
        ans=L
print(ans)