Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i+1]!=t[int(m/gcd)*i+1]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import math
gcd = int(math.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[1+int(m/gcd)*i]!=t[int(n/gcd)*i+1]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import math
gcd = math.gcd(n,m)
lcm = n*m/gcd
l = lcm/((m/gcd)(n/gcd))
counter=0
for i in range(l):
    if s[1+n/gcd]!=t[m/gcd]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)


Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
S = input()
T = input()
import math
gcd_in = (math.gcd(N,M))
lcm_in =  M*N / gcd_in
if gcd_in == 1:
    print(M*M)
elif gcd_in == M or gcd_in == N:
    for i in range(gcd_in):
        counter=0
        if S[(N//gcd_in)*i] == T[(M//gcd_in)*i]:
            counter += 1
    if counter==gcd_in:
        print(max(N,M))
    else:
        print(-1)
else:
    for i in range(int(min(lcm_in/N,lcm_in/M))):
        print(min(lcm_in/N,lcm_in/M))
        counter=0
        if S[int(lcm_in/M*i)] == T[int(lcm_in/N*i)]:
            counter += 1
        if counter==(int(min(lcm_in/N,lcm_in/M))):
            print(lcm_in)
            break
        else:
            print(-1)
        

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
S = input()
T = input()
import math
gcd_in = (math.gcd(N,M))
lcm_in =  M*N / gcd_in
if gcd_in == 1:
    print(M*M)
elif gcd_in == M or gcd_in == N:
    for i in range(gcd_in):
        counter=0
        if S[(N//gcd_in)*i] == T[(M//gcd_in)*i]:
            counter += 1
    if counter==gcd_in:
        print(max(N,M))
    else:
        print(-1)
else:
    for i in range(min(lcm_in/N,lcm_in/M)):
        counter=0
        if S[lcm_in/M*i] == T[lcm_in/N*i]:
            counter += 1
        if counter==(min(lcm_in/N,lcm_in/M)):
            print(lcm_in)
        else:
            print(-1)
        

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().split())
S = input()
T = input()
import math
gcd_in = (math.gcd(N,M))
print(gcd_in)
lcm_in =  M*N / gcd_in
if gcd_in == 1:
    print(M*M)
elif gcd_in == M or gcd_in == N:
    for i in range(gcd_in):
        counter=0
        if S[(N//gcd_in)*i] == T[(M//gcd_in)*i]:
            counter += 1
    if counter==gcd_in:
        print(max(N,M))
    else:
        print(-1)
else:
    for i in range(min(lcm_in/N,lcm_in/M)):
        counter=0
        if S[lcm_in/M*i] == T[lcm_in/N*i]:
            counter += 1
        if counter==(min(lcm_in/N,lcm_in/M)):
            print(lcm_in)
        else:
            print(-1)

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)

Buggy python submission (verdict: Runtime Error):
N,M = map(int,input().stlip())
S = input()
T = input()
import math
gcd_in = (math.gcd(N,M))
lcm_in = (math.lcm(N,M))
if gcd_in == 1:
    print(M*M)
elif gcd_in == M or gcd_in == N:
    for i in range(gcd_in):
    counter=0
        if S[N/gcd_in*i] == T[M/gcd_in*i]:
            counter += 1
    if counter==gcd_in:
        print(max(N,M))
    else:
        print(-1)
else:
    for i in range(min(lcm_in/N,lcm_in/M)):
    counter=0
        if S[lcm_in/M*i] == T[lcm_in/N*i]:
            counter += 1
        if counter==(min(lcm_in/N,lcm_in/M)):
            print(lcm_in)
        else:
            print(-1)
        

Fixed submission:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i]!=t[int(m/gcd)*i]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)