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

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False

ll = (n * m) // math.gcd(x, y)
l = ll

while l <= n * m:
    fail = False
    while not( ns >= l or nt >= l):
        while not(ns <= nt):
            nt += l / m 
            tt += 1

        if(ns == nt) and (s[ss] != t[tt]):
            fail = True
            break

        ns += l / n
        ss += 1
    if not(fail): 
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)


Fixed submission:

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False


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


ll = (n * m) // gcd(n, m)
l = ll

while l <= n * m:
    fail = False
    while not(ns > l or nt > l):
        while not(ns <= nt):
            nt += l / m
            tt += 1

        if(ns == nt) and (s[ss] != t[tt]):
            fail = True
            break

        ns += l / n
        ss += 1
    if not(fail):
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)

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

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

ll = lcm(n,m)
l = ll

while l <= n * m:
    fail = False
    while not( ns >= l or nt >= l):
        while not(ns <= nt):
            nt += l / m 
            tt += 1

        if(ns == nt) and (s[ss] != t[tt]):
            fail = True
            break

        ns += l / n
        ss += 1
    if not(fail): 
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)

Fixed submission:

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False


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


ll = (n * m) // gcd(n, m)
l = ll

while l <= n * m:
    fail = False
    while not(ns > l or nt > l):
        while not(ns <= nt):
            nt += l / m
            tt += 1

        if(ns == nt) and (s[ss] != t[tt]):
            fail = True
            break

        ns += l / n
        ss += 1
    if not(fail):
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)

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

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False

def lcm(x, y):
    return (x * y) // math.gcd(x, y)

ll = lcm(n,m)
l = ll

while l <= n * m:
    fail = False
    while not( ns >= l or nt >= l):
        while not(ns <= nt):
            nt += l / m 
            tt += 1

        if(ns == nt):
            if(s[ss] != t[tt]):
                fail = True
                break

        ns += l / n
        ss += 1
    if not(fail): 
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)


Fixed submission:

n, m = map(int, input().split())
s = input()
t = input()

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False


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


ll = (n * m) // gcd(n, m)
l = ll

while l <= n * m:
    fail = False
    while not(ns > l or nt > l):
        while not(ns <= nt):
            nt += l / m
            tt += 1

        if(ns == nt) and (s[ss] != t[tt]):
            fail = True
            break

        ns += l / n
        ss += 1
    if not(fail):
        break
    cnt += 1
    l = ll * cnt

if(fail):
    print(-1)
else:
    print(l)