Buggy python submission (verdict: Runtime Error):
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
for i in range(N) :
    A.append(i*L//N+1)
for j in range(M) :
    b = j*L//M+1
    if j % (N//M) == 0 :
        index = N*j//M
        if S[index] == T[j] :
            counter += 1
        else :
            flag = 1
            break
    else :
        counter += 1
if flag == 1 :
    print(-1)
else :
    if counter == M :
        print(L)
#print(A)
#print(counter)
#print(flag)

Fixed submission:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
if N >= M :
    for i in range(N) :
        A.append(i*L//N+1)
    for j in range(M) :
        b = j*L//M+1
        if j*N % M == 0 :
            index = N*j//M
            if S[index] == T[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == M :
            print(L)
else :
    for i in range(M) :
        A.append(i*L//M+1)
    for j in range(N) :
        b = j*L//N+1
        if j*M % N == 0 :
            index = M*j//N
            if T[index] == S[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == N :
            print(L)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
for i in range(N) :
    A.append(i*L//N+1)
for j in range(M) :
    b = j*L//M+1
    if b in A :
        index = N*j//M
        if S[index] == T[j] :
            counter += 1
        else :
            flag = 1
    else :
        counter += 1
if flag == 1 :
    print(-1)
else :
    if counter == M :
        print(L)

Fixed submission:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
if N >= M :
    for i in range(N) :
        A.append(i*L//N+1)
    for j in range(M) :
        b = j*L//M+1
        if j*N % M == 0 :
            index = N*j//M
            if S[index] == T[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == M :
            print(L)
else :
    for i in range(M) :
        A.append(i*L//M+1)
    for j in range(N) :
        b = j*L//N+1
        if j*M % N == 0 :
            index = M*j//N
            if T[index] == S[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == N :
            print(L)

Buggy python submission (verdict: Time Limit Exceeded):
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
A = []
B = []
counter = 0
if N % M == 0 or S[0] != T[0] :
    print(-1)
else :
    L = lcm(N,M)
    for i in range(N) :
        A.append(i*L//N+1)
    for j in range(M) :
        B.append(j*L//M+1)
for k in range(len(B)) :
    if B[k] in A :
        for x in range(len(A)) :
            if A[x] == B[k] :
                a = A[k]
        if S[k] == a :
            break
        else :
            counter += 1
    else :
        counter += 1
if counter == len(B) and len(B) != 0 :
    print(L)
    

Fixed submission:
def gcd(m,n) :
    x = max(m,n)
    y = min(m,n)
    if x%y == 0 :
        return y
    else :
        while x%y != 0 :
            z = x%y
            x = y
            y = z
        else :
            return z

def lcm(m,n) :
    return int(m*n/gcd(m,n))

N,M = map(int,input().split())
S = list(input())
T = list(input())
L = lcm(N,M)
A = []
B = []
flag = 0
counter = 0
if N >= M :
    for i in range(N) :
        A.append(i*L//N+1)
    for j in range(M) :
        b = j*L//M+1
        if j*N % M == 0 :
            index = N*j//M
            if S[index] == T[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == M :
            print(L)
else :
    for i in range(M) :
        A.append(i*L//M+1)
    for j in range(N) :
        b = j*L//N+1
        if j*M % N == 0 :
            index = M*j//N
            if T[index] == S[j] :
                counter += 1
            else :
                flag = 1
                break
        else :
            counter += 1
    if flag == 1 :
        print(-1)
    else :
        if counter == N :
            print(L)