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

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

if N==M:
    print(int(N*M)) if S==T else print(-1)
else:
    g = math.gcd(N, M)
    if g==1:
        print(int(N*M))
    else:
        flag = False
        s, t = N//g, M//g
        ss = list(map(lambda x: int(x*s), list(range(0,g))))
        tt = list(map(lambda x: int(x*t), list(range(0,g))))
        for i,j in zip(ss,tt):
            if S[i]!=T[j]:
                flag = True
        if flag:
            print(-1)
        else:
            print(int(s*t*g))

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

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

if S[0]!=T[0]:
    print(-1)
else:
    if N==M:
        print(int(N*M)) if S==T else print(-1)
    else:
        g = gcd(N, M)
        if g==1:
            print(int(N*M))
        else:
            flag = False
            s, t = N//g, M//g
            ss = list(map(lambda x: int(x*s), list(range(0,g))))
            tt = list(map(lambda x: int(x*t), list(range(0,g))))
            for i,j in zip(ss,tt):
                if S[i]!=T[j]:
                    flag = True
            if flag:
                print(-1)
            else:
                print(int(s*t*g))

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

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

if N==M:
    print(int(N*M)) if S==T else print(-1)
else:
    g = math.gcd(N, M)
    if g==1:
        print(int(N*M))
    else:
        flag = False
        s, t = N/g, M/g
        ss = list(map(lambda x: int(x*s), list(range(0,g))))
        tt = list(map(lambda x: int(x*t), list(range(0,g))))
        for i,j in zip(ss,tt):
            if S[i]!=T[j]:
                flag = True
        if flag:
            print(-1)
        else:
            print(int(s*t*g))

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

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

if S[0]!=T[0]:
    print(-1)
else:
    if N==M:
        print(int(N*M)) if S==T else print(-1)
    else:
        g = gcd(N, M)
        if g==1:
            print(int(N*M))
        else:
            flag = False
            s, t = N//g, M//g
            ss = list(map(lambda x: int(x*s), list(range(0,g))))
            tt = list(map(lambda x: int(x*t), list(range(0,g))))
            for i,j in zip(ss,tt):
                if S[i]!=T[j]:
                    flag = True
            if flag:
                print(-1)
            else:
                print(int(s*t*g))

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

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

if N==M:
    print(int(N*M)) if S==T else print(-1)
else:
    g = math.gcd(N, M)
    if g==1:
        print(int(N*M))
    else:
        flag = False
        s, t = N/g, M/g
        ss = list(map(lambda x: int(x*s), list(range(0,g))))
        tt = list(map(lambda x: int(x*t), list(range(0,g))))
        for i,j in zip(ss,tt):
            if S[i]!=T[j]:
                flag = True
        if flag:
            print(-1)
        else:
            print(int(s*t*g))

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

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

if S[0]!=T[0]:
    print(-1)
else:
    if N==M:
        print(int(N*M)) if S==T else print(-1)
    else:
        g = gcd(N, M)
        if g==1:
            print(int(N*M))
        else:
            flag = False
            s, t = N//g, M//g
            ss = list(map(lambda x: int(x*s), list(range(0,g))))
            tt = list(map(lambda x: int(x*t), list(range(0,g))))
            for i,j in zip(ss,tt):
                if S[i]!=T[j]:
                    flag = True
            if flag:
                print(-1)
            else:
                print(int(s*t*g))