TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
# AGC028
from fractions import gcd


def main():
    n, m = tuple(map(int, input().rstrip().split()))
    S, T = tuple(input().rstrip() for _ in range(2))
    l = n // gcd(n, m) * m
    x = [""] * l
    for i in range(n):  # place S in x
        j = (l // n) * i
        x[j] = S[i]

    for i in range(m):  # check T adapts to x or not
        j = (l // m) * i
        if x[j] not in ("", T[i]):
            print(-1)
            break
    else:
        print(l)


if __name__ == "__main__":
    main()

# AGC028A - Two Abbreviations
def gcd(a, b):
    while b > 0:
        a, b = b, a % b
    return a


def main():
    n, m = tuple(map(int, input().rstrip().split()))
    S, T = tuple(input().rstrip() for _ in range(2))
    g = gcd(n, m)
    flg = S[:: n // g] == T[:: m // g]
    print(n // g * m if flg else -1)


if __name__ == "__main__":
    main()

Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n




def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    g = gcd(n, m)
    ans = n * m // g
    a, b = n // g, m // g
    x, y = 0, 0
    for i in range(g):
        if s[x] != x[y]:
            print(-1)
            return
        x += a
        y += b
    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""


# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n




def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    g = gcd(n, m)
    ans = n * m // g
    a, b = n // g, m // g
    x, y = 0, 0
    for i in range(g):
        if s[x] != t[y]:
            print(-1)
            return
        x += a
        y += b
    print(ans)
    
t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
input_lines = input().split()
N = int(input_lines[0])
M = int(input_lines[1])
S = input()
T = input()

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

def lcm(a, b):
  return a*b/gcd(a, b)

L = int(lcm(N,M))
x = 1
count = 0
if N >= M:
    big = N
    small = M
else:
    big = M
    small = N
    
while True:
    k = L*x
    flag = 1
    for j in range(small):
        left = 0
        right = big - 1
        while left <= right:
            mid = (left+right)/2
            if((mid-1)*k/big+1 == (j-1)*k/small+1):
                if(S[mid-1] != T[j-1]):
                    flag = 0
                    count += 1
                    break
                else:
                    break
            elif((mid-1)*k/big+1 > (j-1)*k/small+1):
                right = mid - 1
            else:
                left = mid + 1
    if(flag == 1):
        print(k)
        break
    if(count == 2):
        print(-1)
        break
    x += 1


input_lines = input().split()
N = int(input_lines[0])
M = int(input_lines[1])
S = input()
T = input()

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

def lcm(a, b):
  return a*b/gcd(a, b)

L = int(lcm(N,M))
x = 1
count = 0
while True:
    k = L*x
    flag = 1
    starti = 0
    startj = 0
    for i in range(starti, N):
        for j in range(startj, M):
            hoge = i*k/N+1
            fuga = j*k/M+1
            if(hoge < fuga):
                startj = j - 1
                break
            elif(hoge == fuga):
                if(S[i] != T[j]):
                    flag = 0
                    count += 1
                    break
        if(flag == 0):
            break
    if(flag == 1):
        print(k)
        break
    if(count == 2):
        print(-1)
        break
    x += 1


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
from collections import defaultdict
import math
n,m = map(int, input().split())
s = input()
t = input()

ans = int((n*m)/math.gcd(n,m))
g = math.gcd(n,m)
for i in range(g):
  print (i*(n//g),i*(m//g))
  if s[i*(n//g)] != t[i*(m//g)]:
    print (-1)
    exit()
print (ans)

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

ans = int((n*m)/math.gcd(n,m))
g = math.gcd(n,m)
for i in range(g):
  if s[i*(n//g)] != t[i*(m//g)]:
    print (-1)
    exit()
print (ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd
from sys import stdin

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

    L = lcm(N, M)
    ans = [None] * 10000000000
    for i in range(N):
        j = i * (L // N)
        if ans[j] is None or ans[j] == S[i]:
            ans[j] = S[i]
        else:
            print(-1)
            return
    for i in range(M):
        j = i * (L // M)
        if ans[j] is None or ans[j] == T[i]:
            ans[j] = T[i]
        else:
            print(-1)
            return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()


import platform
ver = platform.python_version_tuple()
if int(ver[1]) < 5:
    from fractions import gcd
else:
    from math import gcd

from sys import stdin
from bisect import bisect

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

    L = lcm(N, M)
    s = [i * (L // N) + 1 for i in range(N)]
    t = [i * (L // M) + 1 for i in range(M)]
    for i, x in enumerate(s):
        n = bisect(t, x)
        if t[n-1] == x:
            if T[n-1] != S[i]:
                print(-1)
                return
    print(L)

input = lambda: stdin.readline().rstrip()
lcm = lambda x, y: x * y // gcd(x, y)
main()


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
N,M = map(int,input().split())
S = input()
T = input()
ans = 0
a = 2

if S[0] != T[0]:ans = -1

elif N % M != 0 and M % N != 0:
    if N >= M:
        while True:
            if N * a % M == 0:
                ans = N * a
                b = int(N*a/M)
                for i in range(M):
                    if T[i] != S[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1
    elif N < M:
        while True:
            if M * a % N == 0:
                ans = M * a
                b = int(M*a/N)
                for i in range(N):
                    if S[i] != T[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1

elif N % M == 0:
    b = int(N/M)
    ans = N
    for i in range(M):
        if T[i] != S[b*i]:
            ans = -1
            break   

elif M % N == 0:
    b = int(M/N)
    ans = M
    for i in range(N):
        if S[i] != T[b*i]:
            ans = -1
            break   

print(ans)


N,M = map(int,input().split())
S = input()
T = input()
ans = 0
a = 2

if S[0] != T[0]:ans = -1

elif N % M != 0 and M % N != 0:
    if N >= M:
        while True:
            if N * a % M == 0:
                ans = N * a
                b = int(N*a/M)
                for i in range(int(N/b)):
                    if T[a*i] != S[b*i]:
                        ans = -1
                        break
                break
            else:
                a += 1
    elif N < M:
        while True:
            if M * a % N == 0:
                ans = M * a
                b = int(M*a/N)
                for i in range(int(M/b)):
                    if T[b*i] != S[a*i]:
                        ans = -1
                        break
                break
            else:
                a += 1

elif N % M == 0:
    b = int(N/M)
    ans = N
    for i in range(M):
        if T[i] != S[b*i]:
            ans = -1
            break   

elif M % N == 0:
    b = int(M/N)
    ans = M
    for i in range(N):
        if S[i] != T[b*i]:
            ans = -1
            break   

print(ans)


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
n,m=map(int,input().split())
s=input()
t=input()

def gcd(a,b):
    if b==0: return a
    return gcd(b,a%b)

G=gcd(n,m)
a,b = n//G,m//G
s=s[1::a]
t=t[1::b]
if s==t: print(a*b*G)
else: print(-1)

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

def gcd(a,b):
    if b==0: return a
    return gcd(b,a%b)

G=gcd(n,m)
a,b = n//G,m//G
s=s[0::a]
t=t[0::b]
if s==t: print(a*b*G)
else: print(-1)

Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

def lcm(a, b):
    return a * b // gcd(a, b)

n, m = map(int, input().split())
s = str(input())
t = str(input())
l = lcc(n, m)
L = [0] * l
for i in range(n):
    L[i*(l//n)] = s[i]
for j in range(m):
    if L[j*(l//m)] == 0:
        L[j*(l//m)] = t[j]
    elif L[j*(l//m)] == t[j]:
        pass
    else:
        print(-1)
        break
else:
    print(l)


def gcd(a, b):
    if a < b:
        a, b = b, a
    while a % b != 0:
        a, b = b, a % b
    return b

def lcm(a, b):
    return a * b // gcd(a, b)

n, m = map(int, input().split())
s = str(input())
t = str(input())
g = gcd(n, m)
l = lcm(n, m)
for i in range(g):
    if s[i*n//g] != t[i*m//g]:
        print(-1)
        break
else:
    print(l)


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

x,y=map(int,input().split())
S=input()
T=input()
GCD=gcd(x,y)
LCM=(x * y) // GCD

x_new=x//GCD
y_new=y//GCD
for i in range(0,GCD-1):
  if S[(x_new)*i]==T[(y_new)*i]:
    result=LCM
  else:
    result=-1
    break
print(result)

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

x,y=map(int,input().split())
S=input()
T=input()
GCD=gcd(x,y)
LCM=(x * y) // GCD

x_new=x//GCD
y_new=y//GCD
for i in range(0,GCD):
  if S[(x_new)*i]==T[(y_new)*i]:
    result=LCM
  else:
    result=-1
    break
print(result)

Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
N,M = map(int,input().split())
S = input()
T = input()
flag = True
def lcm(a, b): 
    def gcd(a, b): 
        while b:
            a, b = b, a % b
        return a
    return a * b // gcd(a, b)
L = lcm(N,M)

if S[0] == T[0 and S[N-1] == T[M-1]:
    flag = True
else:
    flag = False
if flag:
    print(L)
else:
    print(-1)


N,M = map(int,input().split())
S = input()
T = input()
flag = True
def lcm(a, b): 
    def gcd(a, b): 
        while b:
            a, b = b, a % b
        return a
    return a * b // gcd(a, b)
L = lcm(N,M)

if S[0] == T[0] and S[N-1] == T[M-1]:
    flag = True
else:
    flag = False
if flag:
    print(L)
else:
    print(-1)


N,M = map(int,input().split())
S = input()
T = input()
flag = True
def lcm(a, b): 
    def gcd(a, b): 
        while b:
            a, b = b, a % b
        return a
    return a * b // gcd(a, b)
L = lcm(N,M)
def gcd(a, b): 
    while b:
        a, b = b, a % b
    return a

flag = True
for i in range(gcd(N,M)):
    if S[i*N//gcd(N,M)] == T[i*M//gcd(N,M)] and flag:
        pass
    else:
        flag = False
if flag:
    print(L)
else:
    print(-1)


Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a, b):
    if b > a:
        gcd(b, a)
    while b:
	    a, b = b, a % b
    return a

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

l = n * m // gcd(n, m)
a = l // n
b = l // m
#print(a, b)
#a(i-1) = b(j-i) -> ai = bj + (a-b)
res = False
clear_all = True
for i in range(1,n+1):
    if (a*i - (a-b)) % b != 0:
        j = a * i - (a-b)
        if not s[i-1] == t[j-1]:
            clear_all = False
if clear_all:
    res = True
if res:
    print(l)
else:
    print(-1)
        

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

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

l = n * m // gcd(n, m)
a = l // n
b = l // m
#print(a, b)
#a(i-1) = b(j-i) -> ai = bj + (a-b)
res = False
clear_all = True
for i in range(1,n+1):
    if (a*i - (a-b)) % b == 0:
        j = (a * i - (a-b)) // b
        if not s[i-1] == t[j-1]:
            clear_all = False
if clear_all:
    res = True
if res:
    print(l)
else:
    print(-1)
        

Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
from math import gcd
N,M = map(int,input().split())
s = input()
t = input()
g = gcd(N,M)
l = N*M//g

n = N//g
m = M//g

ans = l

for i in range(l):
    if s[i*n]!=t[i*m]:
        ans = -1
        break
print(ans)



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

flag = True
for n in range(N):
    if (M*n)%N==0:
        m = (M*n)//N
        flag = (S[n]==T[m])
        if !flag:
            break
if flag:
    print(N*M//math.gcd(N,M))
else:
    print(-1)


def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
    
N,M = map(int,Input().split())
s = input()
t = input()
g = gcd(N,M)
l = N*M//g

n = N//g
m = M//g

ans = l

for i in range(g):
    if s[i*n]!=t[i*m]:
        ans = -1
        break
print(ans)

def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
N,M = map(int,input().split())
S = input()
T = input()
 
flag = True
for n in range(N):
    if M*n%N==0:
        m = M*n//N
        flag = S[n]==T[m]
    if not flag:
        break
if not flag:
    print(-1)
else:
    print(N*M//gcd(N,M))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a,b):
    if b == 0:
        return abs(a)
    else:
        return gcd(b, a%b)

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

print(-1)
exit()


import bisect

def gcd(a,b):
    if b == 0:
        return abs(a)
    else:
        return gcd(b, a%b)

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


if S[0] != T[0]:
    print(-1)
    exit()

if len(S) == len(T):
    if S == T:
        print(len(S))
    else:
        print(-1)
    exit()

gcd1 = gcd(len(S), len(T))
lcm = len(S)*len(T) // gcd1

SS = []
TT = []
i = 0
j = 0
while i < N:
    SS.append(i*lcm//N + 1)
    i += 1
while j < M:
    TT.append(j*lcm//M + 1)
    j += 1

ind = 0
ind_max = j-1

for i,v in enumerate(SS):
    ind = bisect.bisect_left(TT, v,ind, j)
    if ind == j:
        break
#    print(ind)
    if TT[ind] == v:
        if S[i] == T[ind]:
            continue
        else:
            print(-1)
            exit()
print(lcm)


Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import sys
N,M = [int(n) for n in input().split()]
S = str(input())
T = str(input())

# N < M
def gcd(N,M):
    if(N > M):
        tmp = N
        N = M
        M = tmp
    r = M % N
    while(r != 0):
        M = N
        N = r
        r = M%N
    return N

def lcm(N,M):
    return(N*M/gcd(N,M))

if (N % M == 0):
    if S == T:
        print(M)
    else:
        p = int(N/M)
        for i in range(M):
            if T[i] != S[i*p]:
                print(-1)
                sys.exit()
        print(N)
elif (M % N == 0):
    if S == T:
        print(M)
    else:
        p = int(M/N)
        for i in range(N):
            if S[i] != T[i*p]:
                print(-1)
                sys.exit()
        print(M)
else:

    print(-1)


import sys
N,M = [int(n) for n in input().split()]
S = str(input())
T = str(input())

# N < M
def gcd(N,M):
    if(N > M):
        tmp = N
        N = M
        M = tmp
    r = M % N
    while(r != 0):
        M = N
        N = r
        r = M%N
    return N

def lcm(N,M):
    return(N*M/gcd(N,M))

if (N % M == 0):
    if S == T:
        print(M)
    else:
        p = int(N/M)
        for i in range(M):
            if T[i] != S[i*p]:
                print(-1)
                sys.exit()
        print(N)
elif (M % N == 0):
    if S == T:
        print(M)
    else:
        p = int(M/N)
        for i in range(N):
            if S[i] != T[i*p]:
                print(-1)
                sys.exit()
        print(M)
else:
    mngcd = gcd(N,M)
    k = int(N/mngcd)
    m = int(M/mngcd)
    for i in range(mngcd):
        if S[i*k] != T[i*m]:
            print(-1)
            sys.exit()
    print(int(lcm(N,M)))


Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import sys
from fractions import gcd
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().split())
    S = readline().decode('utf-8')
    T = readline().decode('utf-8')
    lcm = N*M//gcd(N,M)
    if lcm==N or lcm==M:
        print(-1)
    else:
        print(lcm)


if __name__ == '__main__':
    main()

import sys
import math
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, readline().split())
    S = readline().rstrip().decode('utf-8')
    T = readline().rstrip().decode('utf-8')

    G = math.gcd(N, M)

    A = ''.join(S[::N // G])
    B = ''.join(T[::M // G])

    if A == B:
        answer = N * M // G
    else:
        answer = -1
    print(answer)


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
print(-1)

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

N,M = map(int,input().split(" "))
S = input()
T = input()
N = len(S)
M = len(T)
x = gcd(N,M)
y = int(N*M/x)

for i in range(x):
    if S[int(i*y/M)] == T[int(i*y/N)]:
        continue
    else:
        print(-1)
        exit()
print(y)


Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import fraction
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

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

lcm_num = lcm(N,M)

X_s = ["" for i in range(lcm_num)]
X_t = ["" for i in range(lcm_num)]

# print(X_s,X_t)

for i in range(N):
    tmp = i*lcm_num // N + 1
    X_s[tmp-1] = S[i]


for i in range(M):
    tmp = i*lcm_num // M + 1
    X_t[tmp-1] = T[i]

# print(X_s,X_t)

for s,t in zip(X_s,X_t):
    if s == "" or t == "" :
        continue
    elif s == t:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


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

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

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

lcm_num = lcm(N,M)
lN = lcm_num // N
lM = lcm_num // M
gcd_num = lN * lM

for i in range(0,lcm_num,gcd_num):
    if S[i//lN] == T[i//lM]:
        continue
    else:
        print(-1)
        exit()

print(lcm_num)


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a, b):
    c = a%b
    while(c != 0):
        a = b
        b = c
        c = a%b
    return b

def answer(s, t):
    a = len(s)
    b = len(t)
    if a[0] != b[0]:
        return -1
    if a < b:
        temp = a
        a = b
        b = temp
    gcdAB = gcd(a, b)
    if gcdAB == 1:
        return a*b
    else:
        stack = 0
        sl = (gcdAB * (a//gcdAB) * (b//gcdAB))
        aJump = sl//a
        bJump = sl//b
        for i in range(b):
            if stack%aJump == 0:
                if s[stack//aJump] != t[i]:
                    return -1
            stack = bJump*(i+1)
        return sl
                

sl, tl = map(int, input().split())
s = input()
t = input()
print(answer(s, t))


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

def answer(s, t):
    a = len(s)
    b = len(t)
    if s[0] != t[0]:
        return -1
    if a < b:
        temp = a
        a = b
        b = temp
        temp = s
        s = t
        t = temp
    gcdAB = gcd(a, b)
    if gcdAB == 1:
        return a*b
    else:
        stack = 0
        sl = (gcdAB * (a//gcdAB) * (b//gcdAB))
        aJump = sl//a
        bJump = sl//b
        for i in range(b):
            if stack%aJump == 0:
                if s[stack//aJump] != t[i]:
                    return -1
            stack = bJump*(i+1)
        return sl
                

sl, tl = map(int, input().split())
s = input()
t = input()
print(answer(s, t))


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def gcd(a, b):  
  if b == 0: return a
  return gcd(b, a % b)
n, m =  map(int, input().split())
g = gcd(n, m)
l = n * m / g
n = n/g
m = m/g
s = list(str(input()))[::int(n)]
t = list(str(input()))[::int(m)]
if s == t:
  print(l)
else:
  print("-1")

def gcd(a, b):  
  if b == 0: return a
  return gcd(b, a % b)
n, m =  map(int, input().split())
g = gcd(n, m)
l = n * m / g
n = n/g
m = m/g
s = list(str(input()))[::int(n)]
t = list(str(input()))[::int(m)]
if s == t:
  print(l)
else:
  print("-1")

def gcd(a, b):  
  if b == 0: return a
  return gcd(b, a % b)
n, m =  map(int, input().split())
g = gcd(n, m)
l = n * m // g
n = n//g
m = m//g
s = list(str(input()))[::int(n)]
t = list(str(input()))[::int(m)]
if s == t:
  print(l)
else:
  print("-1")

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import math
N,M= list(map(int,input().split()))
S =input()
T=input()


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

d = dict()

for i in range(N):
    print(1+(L//N)*i)
    index = 1+(L//N)*i
    d[index] = S[i]

for j in range(M):
    index = (1+(L//M)*j)
    if index in d:
        if d[index]!=T[j]:
            print(-1)
            exit()
print(L)




import math

N, M = list(map(int, input().split()))
S = input()
T = input()
def gcd(a, b):
    if a < b:
        a, b = b, a
    if b == 0:
        return a
    c = a % b
    return gcd(b, c)

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

d = dict()

for i in range(N):
    index = 1 + (L // N) * i
    d[index] = S[i]

for j in range(M):
    index = (1 + (L // M) * j)
    if index in d:
        if d[index] != T[j]:
            print(-1)
            exit()
print(L)


Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
N, M =  map(int, input().split(' '))
S = input()
T = input()
import numpy as np
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(x, y):
    return (x * y) // gcd(x, y)
 
L = lcm(N,M)
#max_n = max(N,M)

x1 = [1] + [(i *(L/N)) +1for i in range(1, N)] 
x2 = [1]+ [(i *(L/M)) +1for i in range(1, M)] 
x3 = set(x1) & set(x2) 
if len(x3) ==1:
   print("a") 
   print(L)
else :
    if all([S[int((i-1)/(L/N))] == T[int((i -1)/(L/M))] for i in x3 if i != 1]) :
        print(L)
    else :
        print(-1)

N, M =  map(int, input().split(' '))
def gcd(a, b):
    a, b = (a, b) if a >= b else (b, a)
    while b > 0:
        a, b = b, a % b
    return a

S = input()
T = input()
g = gcd(N, M)
#print('gcd',g)
#print('',N//g,M//g)
#print(S[0::N//g] ,T[0::M//g])
if S[0::N//g] == T[0::M//g]:
    print(N*M//g)
else:
    print(-1)

Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):

def gcd(a, b):
    while b:
            a,b=b,a%b
    return a
def lcm(x,y):
    return (x * y) // gcd(x, y)

def atwo(n,m,s,t):
    l=lcm(n,m)
    gc=gcd(n,m)
    if s[0::n//gc]==t[0::m//gc]:
        print(l)
        exit()
    else:
        print(-1)
        exit()

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

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

def atwo(n,m,s,t):
    l=lcm(n,m)
    gc=gcd(n,m)
    if s[0::n//gc]==t[0::m//gc]:
        print(l)
        exit()
    else:
        print(-1)
        exit()

n,m=map(int,input().split())
print(atwo(n,m,input(),input()))

Verdicts: Wrong Answer

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import math

 

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

pair = {N:S,M:T}


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    if gcd(n,m)==1:
        return lcd(n,m)
    global pair
    n_i = int(n/gcd(n,m))
    m_i = int(m/gcd(n,m))
    n_list = pair[n][::n_i]
    m_list = pair[m][::m_i]
#    print(c)

    if n_list[0:len(m_list)]==m_list:
        return lcd(n,m)
    else:
        return -1
n = max(pair.keys())
m = min(pair.keys())
if pair[N][0]!=pair[M][0]:
    print(-1)
elif N==M:
    if S==T:
        print(N)
    else
        print(-1)
else:
    print(prove(n,m))



import math

 

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


def lcd(n,m):
    return int(n * m / gcd(n,m))
def gcd(n,m):
    if n % m == 0:
        return m
    else:
        return gcd(m,n % m)
#    return int(m*n/math.gcd(n,m))
    
def prove(n,m):
    n_i = int(lcd(n,m)/n)
    m_i = int(lcd(n,m)/m)
    n_list=[S[m_i*i] for i in range(gcd(n,m))]
    m_list=[T[n_i*i] for i in range(gcd(n,m))]
#    print(c)

    if n_list==m_list:
        return lcd(n,m)
    else:
        return -1


print(prove(N,M))



Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
n,m=map(int,input().split())
s=input()
t=input()

def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y

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

L=lcm(n,m)
g=gcd(n,m)
if s[0]==t[0]:
  X=[""]*(L+1)
  print(X)
else:
  print(-1)
  exit()
for i in range(1,g):
  X[int(i*L/n)+1]=s[i]
for i in range(1,g):
  if X[int(i*L/m)+1] !="":
    if X[int(i*L/m)+1] !=t[i]:
      print(-1)
      exit()
print(L)

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

def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y

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

L=lcm(n,m)
if s[0]==t[0]:
  X=[""]*L+1
else:
  print(-1)
  exit()
for i in range(1,n):
  X[int(i*L/n)+1]=s[i]
for i in range(1,m):
  if X[int(i*L/m)+1] !="":
    if X[int(i*L/m)+1] !=t[i]:
      print(-1)
      exit()
print(L)

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

def gcd(x,y):
  r=x%y
  return gcd(y,r) if r else y

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

L=lcm(n,m)
g=gcd(n,m)
for i in range(g):
  if s[int(i*n/g)]!=t[int(i*m/g)]:
    print(-1)
    exit()
print(L)

Verdicts: Runtime Error

Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
import numpy as np# your code goes here
def eucrid(a, b):
	if (a > b):
		buf = a
		a = b
		b = buf
	if (b % a == 0):
		return a
	else:
		return eucrid(b%a, a)
		
def main():
	buf = input().split()
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	
	big = n*m//eucrid(n, m)
	flag = big
	for i in range(big):
		if (i%(big//n) == 0 and i%(big//m) == 0):
			print(i, n, m)
			print((i+1)//(big//n), (i+1)//(big//m))
			if(s[(i+1)//(big//n)] != t[(i+1)//(big//m)]):
				flag = -1
				
	print(flag)
	#print(s, t, n, m)
	
	
main()

import numpy as np# your code goes here
def eucrid(a, b):
	if (a > b):
		buf = a
		a = b
		b = buf
	if (b % a == 0):
		return a
	else:
		return eucrid(b%a, a)
		
def main():
	buf = input().split()
	s = input()
	t = input()
	n = len(s)
	m = len(t)
	
	big = n*m//eucrid(n, m)
	flag = big
	"""
	for i in range(big):
		if (i%(big//n) == 0 and i%(big//m) == 0):
			#print(i, n, m)
			#print((i+1)//(big//n), (i+1)//(big//m))
			if(s[(i+1)//(big//n)] != t[(i+1)//(big//m)]):
				flag = -1
	"""			
	for i in range(n):
		if((i*(big//n))%(big//m) == 0):
			if(s[i] != t[(i*(big//n))//(big//m)]):
				flag = -1

	print(flag)
	#print(s, t, n, m)
	
	
main()

Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
def main():
    import sys
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = (N * M) // gcd(N, M)
    ans = ['0'] * L
    for n in range(L//N):
        ans[L//N*n] = S[n%N]
    for m in range(L//M):
        if ans[(L//M)*m] == '0':
            ans[(L//M)*m] = T[m%M]
        elif ans[(L//M)*m] == T[m%M]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

def main():
    import sys
    from collections import defaultdict
    sys.setrecursionlimit(10**6)
    input = sys.stdin.readline

    def gcd(a, b):
        if b == 0:
            return a
        return gcd(b, a % b)
    
    N, M = [int(x) for x in input().strip().split()]
    S = input().strip()
    T = input().strip()
    L = (N * M) // gcd(N, M)
    d = defaultdict(int)
    for n in range(N):
        d[L//N*n] = S[n]
    for m in range(M):
        if d[L//M*m] == 0 or d[L//M*m] == T[m]:
            continue
        else:
            print(-1)
            return
    print(L)

if __name__ == '__main__':
    main()

Verdicts: Runtime Error

Accepted
Problem: You are given a string S of length N and another string T of length M. These strings consist of lowercase English letters.  A string X is called a **good string** when the following conditions are all met:    * Let L be the length of X. L is divisible by both N and M.   * Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.   * Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.  Determine if there exists a good string. If it exists, find the length of the shortest such string.

Submission attempts (in order):
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*L

i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i += 1
    print(lis)
    
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            ans = -1
            break
    print(lis)
else:
    ans = L
print(ans)

#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M


for i in range(GCD):
    
    s = dist_M * i
    t = dist_N * i
    if S[s] != T[t]:
        ans = -1
        break
else:
    ans = L
print(ans)

Verdicts: Runtime Error

Accepted