Problem:


Score : 300 points

Problem Statement

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.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45



Sample input:
3 2
acp
ae


Sample output:
6

Submission status: Runtime Error

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

from fractions import gcd
L = (N * M) // gcd(N, M)

L1 = [1]
L2 = [1]
# for i in range(1, N):
#     t = i * (L // N) + 1
#     L1.append(t)

# for i in range(1, M):
#     t = i * (L // M) + 1
#     L2.append(t)

LS = [None] * L
# for i, li in enumerate(L1):
#     ind = li-1
#     LS[ind] = S[i]

ans = True
# for i, li in enumerate(L2):
#     ind = li-1
#     if LS[ind] is not None:
#         if LS[ind] != T[i]:
#             ans = False

if ans:
    print(L)
else:
    print(-1)


# print(L1)
# print(L2)
# print(LS)

Submission status: Runtime Error

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

from fractions import gcd
L = (N * M) // gcd(N, M)

L1 = [1]
L2 = [1]
for i in range(1, N):
    t = i * (L // N) + 1
    L1.append(t)

for i in range(1, M):
    t = i * (L // M) + 1
    L2.append(t)

LS = [None] * L
# for i, li in enumerate(L1):
#     ind = li-1
#     LS[ind] = S[i]

ans = True
# for i, li in enumerate(L2):
#     ind = li-1
#     if LS[ind] is not None:
#         if LS[ind] != T[i]:
#             ans = False

if ans:
    print(L)
else:
    print(-1)


# print(L1)
# print(L2)
# print(LS)

Submission status: Runtime Error

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

from fractions import gcd
L = (N * M) // gcd(N, M)

L1 = [1]
L2 = [1]
for i in range(1, N):
    t = i * (L // N) + 1
    L1.append(t)

for i in range(1, M):
    t = i * (L // M) + 1
    L2.append(t)



LS = [None] * L
for i, li in enumerate(L1):
    ind = li-1
    LS[ind] = S[i]

ans = True
# for i, li in enumerate(L2):
#     ind = li-1
#     if LS[ind] is not None:
#         if LS[ind] != T[i]:
#             ans = False

if ans:
    print(L)
else:
    print(-1)


# print(L1)
# print(L2)
# print(LS)

Submission status: Runtime Error

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

from fractions import gcd
L = (N * M) // gcd(N, M)

L1 = [1]
L2 = [1]
for i in range(1, N):
    t = i * (L // N) + 1
    L1.append(t)

for i in range(1, M):
    t = i * (L // M) + 1
    L2.append(t)

common = []

# print(L1)
# print(L2)

LS = [None] * L
for i, li in enumerate(L1):
    ind = li-1
    LS[ind] = S[i]

ans = True
for i, li in enumerate(L2):
    ind = li-1
    if LS[ind] is not None:
        if LS[ind] != T[i]:
            ans = False

if ans:
    print(L)
else:
    print(-1)

Submission status: Runtime Error

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

from fractions import gcd
L = (N * M) // gcd(N, M)

L1 = [1]
L2 = [1]
for i in range(1, N):
    t = i * (L // N) + 1
    L1.append(t)

for i in range(1, M):
    t = (L // M) + 1
    L2.append(t)

common = []

# print(L1)
# print(L2)

LS = [None] * L
for i, li in enumerate(L1):
    ind = li-1
    LS[ind] = S[i]

ans = True
for i, li in enumerate(L2):
    ind = li-1
    if LS[ind] is not None:
        if LS[ind] != T[i]:
            ans = False

if ans:
    print(L)
else:
    print(-1)

Submission status: Runtime Error

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

n=N//math.gcd(N,M)
m=M//math.gcd(N,M)
g=math.gcd(N,M)
judge=True

for i in range(g):
    if S[i*n]!=T[i*m]:
        judge=False

if judge==True:
    print(N*M//g)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
S = input()
T = input()
f = fractions.gcd(N, M)
flag = True
for i in range(f):
    if S[i*N//f] != T[i*M//f]:
        flag = False
        break
print([-1,N*M//f][flag])

Submission status: Runtime Error

Solution code:
import math

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

s_num = [0] * n
t_num = [0] * m
s_num[0], t_num[0] = 1, 1
l = n * m / math.gcd(n, m)

for i in range(1, n):
    s_num[i] = i * l / n + 1

for i in range(1, m):
    t_num[i] = i * l / m + 1

match_num = list(set(s_num) & set(t_num))
ans = l

for i in range(len(match_num)):
    s_str = s[s_num.index(match_num[i])]
    t_str = t[t_num.index(match_num[i])]

    if s_str != t_str:
        print('-1')
        exit()

print(int(ans))

Submission status: Runtime Error

Solution code:
import math

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

s_num = [0] * n
t_num = [0] * m
s_num[0], t_num[0] = 1, 1
l = n * m / math.gcd(n, m)

for i in range(1, n):
    s_num[i] = i * l / n + 1

for i in range(1, m):
    t_num[i] = i * l / m + 1

match_num = list(set(s_num) & set(t_num))
ans = l

for i in range(len(match_num)):
    s_str = s[s_num.index(match_num[i])]
    t_str = t[t_num.index(match_num[i])]

    if s_str != t_str:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import math

input = sys.stdin.readline

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

s_num = [0] * n
t_num = [0] * m
s_num[0], t_num[0] = 1, 1
l = n * m / math.gcd(n, m)

for i in range(1, n):
    s_num[i] = i * l / n + 1

for i in range(1, m):
    t_num[i] = i * l / m + 1

match_num = list(set(s_num) & set(t_num))
ans = l

for i in range(len(match_num)):
    s_str = s[s_num.index(match_num[i])]
    t_str = t[t_num.index(match_num[i])]

    if s_str != t_str:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
#最大公約数
def gcd(a,b):
    while b!=0:
        a,b = b, a%b
    return a

#最小公倍数
def lcm(a,b):
    return a*b //gcd(a,b)

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

l = lcm(n,m)
txt = ['.']*l
res = l
for i in range(n):
    txt[i*(l//n)] = s[i]
for i in range(m):
    if (txt[i*(l//m)]!='.'):
        if (txt[i*(l//m)]!=t[i]):
            res = -1
            break
print(res)

Submission status: Runtime Error

Solution code:
#最大公約数
def gcd(a,b):
    while b!=0:
        a,b = b, a%b
    return a

#最小公倍数
def lcm(a,b):
    return a*b //gcd(a,b)

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

l = lcm(n,m)
txt = ['.']*l

res = l
for i in range(n):
    txt[i*(l//n)] = s[i]
for i in range(m):
    if txt[i*(l//m)]!='.' and txt[i*(l//m)]!=t[i]:
        res = -1
        break
print(res)

Submission status: Runtime Error

Solution code:
#最大公約数
def gcd(a,b):
    while b!=0:
        a,b = b, a%b
    return a

#最小公倍数
def lcm(a,b):
    return a*b //gcd(a,b)

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

l = lcm(n,m)
txt = ['.']*l

res = l
for i in range(n):
    txt[i*(l//n)] = s[i]
for i in range(m):
    if txt[i*(l//m)]=='.' or txt[i*(l//m)]==t[i]:
        continue
    else:
        res = -1
print(res)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？

n,m=mii()
s=ii()
t=ii()

p=gcd(s,t)
if p!=s and p!=t and s[0]==p[0]:
    print(p)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from fractions import gcd
def lcm(x,y):
    return x*y//gcd(x,y)
n,m = map(int,input().split())
s=input()
t=input()
ss=''
tt=''

l=lcm(n,m)
x=['*']*l

for i in range(n):
    x[i*(l//n)]=s[i]

for i in range(m):
    x[i*(l//m)]=t[i]

for i in range(n):
    if x[i*(l//n)]!=s[i]:
        print(-1)
        exit()

for i in range(m):
    if x[i*(l//m)]!=t[i]:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
from fractions import gcd
def lcm(x,y):
    return x*y//gcd(x,y)
n,m = map(int,input().split())
s=input()
t=input()
ss=''
tt=''

l=lcm(n,m)
x=['x']*l

for i in range(n):
    x[i*(l//n)]=s[i]

for i in range(m):
    x[i*(l//m)]=t[i]

for i in range(n):
    if x[i*(l//n)]!=s[i]:
        print(-1)
        exit()

for i in range(m):
    if x[i*(l//m)]!=t[i]:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
from math import gcd
def lcm(x,y):
    return x*y//gcd(x,y)
n,m = map(int,input().split())
s=input()
t=input()
ss=''
tt=''

l=lcm(n,m)
x=['x']*l

for i in range(n):
    x[i*(l//n)]=s[i]

for i in range(m):
    x[i*(l//m)]=t[i]

for i in range(n):
    if x[i*(l//n)]!=s[i]:
        print(-1)
        exit()

for i in range(m):
    if x[i*(l//m)]!=t[i]:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
from fractions import gcd
n,m = map(int,input().split())
s = input()
t = input()
x = gcd(n,m)
for i in range(x+1):
  if s[i*n//x]!=t[i*m//x]:
    print(-1)
    break
else:
  print(n*m//x)

Submission status: Runtime Error

Solution code:
import math
import sys

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

if s[0] != t[0]:
	print(-1)
	sys.exit()

GCD = math.gcd(n,m)

if GCD == 1:
	print(n*m)
	sys.exit()

LCM = n*m//GCD


s_k = LCM//m
t_k = LCM//n
for i in range(GCD):
	if s[i*s_k] != t[i*t_k]:
		print(-1)
		sys.exit()

print(LCM)

Submission status: Runtime Error

Solution code:
import math
import sys

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

if s[0] != t[0]:
	print(-1)
	sys.exit()

GCD = math.gcd(n,m)

if GCD == 1:
	print(n*m)
	sys.exit()

LCM = n*m//GCD


s_k = LCM//m
t_k = LCM//n
for i in range(GCD):
	if s[i*s_k] != t[i*t_k]:
		print(-1)
		sys.exit()

print(LCM)

Submission status: Runtime Error

Solution code:
from fractions import gcd

N, M = map(int,input().split())
S = input()
T = input()
L = (N*M)//gcd(N,M)
ans = [0] * (L+1)
for i in range(1,N+1):
    ans[1+(i-1)*(L//N)] = S[i-1]

for i in range(1,M+1):
    if ans[1+(i-1)*(L//M)] == 0:
        ans[1+(i-1)*(L//M)] = T[i-1]
    else:
        if ans[1+(i-1)*(L//M)] == T[i-1]:
            pass
        else:
            print(-1)
            exit()

print(L)

Submission status: Runtime Error

Solution code:
from fractions import gcd

N, M = map(int,input().split())
S = input()
T = input()
L = N*M//gcd(N,M)
ans = [0] * (L+1)
for i in range(1,N+1):
    ans[1+(i-1)*(L//N)] = S[i-1]

for i in range(1,M+1):
    if ans[1+(i-1)*(L//M)] == 0:
        ans[1+(i-1)*(L//M)] = T[i-1]
    else:
        if ans[1+(i-1)*(L//M)] == T[i-1]:
            pass
        else:
            print(-1)
            exit()

print(L)

Submission status: Runtime Error

Solution code:
import fractions

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

l = n*m//fractions.gcd(n, m)

s_dic = {i * l//n + 1: s[i] for i in range(n)}
t_dic = {i * l//m + 1: t[i] for i in range(m)}

dup_idx = set(s_dic.keys) & set(t_dic.keys)

for d in dup_idx:
    if s_dic[d] != t_dic[d]:
        print(-1)
        break
else:
    print(l)

Submission status: Runtime Error

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

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

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

x = [0] * l
a, b = [], []

for i in range(n):
    x[i*(l//n)] = s[i]
    a.append(i*(l//n))

for j in range(m):
    x[j*(l//m)] = t[j]
    b.append(j*(l//m))

if "".join([x[i] for i in a]) == s and ("".join([x[j] for j in b])) == t:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

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

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

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

x = [" "] * l
a, b = [], []
for i in range(n):
    x[i*(l//n)] = s[i]
    a.append(i*(l//n))
for j in range(m):
    x[j*(l//m)] = t[j]
    b.append(j*(l//m))

if "".join([x[i] for i in a]) == s and ("".join([x[j] for j in b])) == t:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)

def main():
	N, M = MAP()
	S = input().rstrip()
	T = input().rstrip()

	L = lcm(N, M)
	n = int(L//N)
	m = int(L//M)
	X = ['0']*L
	for i in range(0, L, n):
		X[i] = S[i//n]
	for i in range(0, L, m):
		if X[i] != '0' and X[i] != T[i//m]:
			print(-1)
			sys.exit()
	print(L)

if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

#a,bの最大公約数
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#a,bの最小公倍数
def lcm(a, b):
    return a * b // gcd (a, b)

def main():
	N, M = MAP()
	S = input().rstrip()
	T = input().rstrip()

	L = lcm(N, M)
	n = int(L//N)
	m = int(L//M)
	X = ['0']*L
	for i in range(N):
		X[i*n] = S[i]
	for i in range(M):
		if X[i*m] != '0' and X[i*m] != T[i]:
			print(-1)
			sys.exit()
	print(L)

if __name__ == '__main__':
	main()

Submission status: Runtime Error

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
if gcd(n,m)==1:
  if s[0]==t[0]:
    print(n*m)
  else:
    print(-1)
else:
  k=n//gcd(n,m)
  l=m//gcd(n,m)
  if len(s)<=len(t):
    for i in range(k):
      if s[i*k]!=t[i*l]:
        print(-1)
        exit()
    print(k*l*gcd(n,m))
  else:
    for i in range(l):
      if s[i*k]!=t[i*l]:
        print(-1)
        exit()
    print(k*l*gcd(n,m))

Submission status: Runtime Error

Solution code:
def gcd(x,y):
    if x<y:
        x,y=y,x
    if y==0:
        return x
    else:
        return gcd(y,x%y)

N,M=map(int,input().split())
S=input()
T=input()
L=N*M//gcd(N,M)
n=L//N
m=L//M
l=n*m//gcd(n,m)
i=l//n
j=l//m
for k in range(N//i):
    if S[i*k]==T[j*k]:
        continue
    else:
        print(-1)
        sys.exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
def gcd(a,b):
    if a >= b:
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    else:
        if a == 0:
            return b
        else:
            return gcd(a,b%a)
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    flag = True
    L = int((N//gcd(N,M))*M)
    m = L//M
    n = L//N
    for i in range(min(N,M)):
        if i*m <= N-1 and i*n <= M-1:
            if S[i*m*L//N] != T[i*n*L//M]:
                flag = False
                break
    if flag:
        print(L)
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
def gcd(a,b):
    if a >= b:
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    else:
        if a == 0:
            return b
        else:
            return gcd(a,b%a)
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    flag = True
    if N//M >= 2:
        for i in range(M):
            x = (N*i)//M
            if T[x*(L//M)+1] != S[i*(L//N)+1]:
                flag = False
    if M//N >= 2:
        for i in range(N):
            y = (N*i)//M
            if T[i*(L//M)+1] != S[y*(L//N)+1]:
                flag = False
    if flag:
        print(int((N//gcd(N,M))*M))
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
import fractions as f
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//f.gcd(n,m))
d=(n*m)//f.gcd(n,m)
N=n//f.gcd(n,m)
M=m//f.gcd(n,m)
for i in range(f.gcd(n,m)):
    if s[i*N]!=t[i*M]:
        ans=-1
        break
if ans!=-1:
    print(d)
else:
    print(-1)
#print(cnt)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//fractions.gcd(n,m))
d=(n*m)//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print(d)
else:
    print(-1)
#print(cnt)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*((n*m)//fractions.gcd(n,m))
d=(n*m)//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print((n*m)//fractions.gcd(n,m))
else:
    print(-1)
#print(cnt)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*(n*m//fractions.gcd(n,m))
d=n*m//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
    else:
        cnt[j*d//m]=t[j]
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(-1)
print(cnt)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
cnt=[""]*(n*m//fractions.gcd(n,m))
d=n*m//fractions.gcd(n,m)
for i in range(n):
    cnt[i*d//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*d//m] and cnt[j*d//m]!="":
        ans=-1
        break
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(ans)
#print(cnt)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0

cnt=[""]*(n*m//fractions.gcd(n,m))
for i in range(n):
    cnt[i*max(n,m)//n]=s[i]
for j in range(m):
    if t[j]!=cnt[j*max(n,m)//m]:
        ans=-1
        break
if ans!=-1:
    print(n*m//fractions.gcd(n,m))
else:
    print(ans)

Submission status: Runtime Error

Solution code:
from fractions import gcd


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


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

if s[::(lcm/m)]!=t[::(lcm/n)]:
    print(-1);exit()

print(lcm)

Submission status: Runtime Error

Solution code:
import sys

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

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

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

L = lcm(N, M)
X = ["A"] * L

    
print(-20)
sys.exit()


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

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Submission status: Runtime Error

Solution code:
import sys

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

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

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

L = lcm(N, M)
X = ["A"] * L

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]
    
print(-20)
sys.exit()

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Submission status: Runtime Error

Solution code:
import sys

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

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

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

L = lcm(N, M)
X = ["A"] * L

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

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Submission status: Runtime Error

Solution code:
import sys

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

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

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

L = lcm(N, M)
X = ["A"] * L

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

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Submission status: Runtime Error

Solution code:
import sys

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

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

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

L = lcm(N, M)
X = ["A"] * L

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

for i in range(M):
    index = i * (L // M) + 1
    if X[index-1] != "A" and X[index-1] != T[i]:
        print(-1)
        sys.exit()
    X[index-1] = T[i]
    
print(L)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(1,p):
    if i%n==i%m==0:
        if s[i//m]!=t[i//n]:
        print(-1)
        exit()
print(p)

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(1,p):
    if i%n==i%m==0:
        if s[i//m]!=l[i//n]:
        print(-1)
        exit()
print(p)

Submission status: Runtime Error

Solution code:
# 2019-11-28 02:28:10(JST)
import sys

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

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

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t = sys.stdin.read().split()

    g = gcd(n, m)
    l = lcm(n, m)

    if g == 1:
        if s[0] == s[1]:
            ans = l
        else:
            ans = -1
    else:
        for i in range(g):
            if s[i * n // g] == t[i * m // g]:
                continue
            else:
                ans = -1
                break
        else:
            ans = l
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-28 02:28:10(JST)
import sys

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

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

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t = sys.stdin.read().split()

    g = gcd(n, m)
    l = lcm(n, m)

    if g == 1:
        if s[0] == s[1]:
            ans = l
        else:
            an = -1
    else:
        for i in range(g):
            if s[i * n // g] == t[i * m // g]:
                continue
            else:
                ans = -1
                break
        else:
            ans = l
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# 2019-11-28 02:28:10(JST)
import sys

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

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

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t = sys.stdin.read().split()

    g = gcd(n, m)
    l = lcm(n, m)

    if g == 1:
        if s[0] == s[1]:
            ans = l
    else:
        for i in range(g):
            if s[i * n // g] == t[i * m // g]:
                continue
            else:
                ans = -1
                break
        else:
            ans = l
    
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


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


def coprime(a, b):
    return gcd(a, b) == 1


def main():
    N, M = MI()
    S = input()
    T = input()

    LMC = lcm(N, M)

    if coprime(N, M):
        if S[0] == T[0]:
            print(LMC)
        else:
            print(-1)
    else:
        X = [None] * LMC
        for i in range(N):
            if i == 0:
                X[i] = S[i]
            else:
                X[i * LMC//N] = S[i]

        # for i in range(LMC):
        #     print(i, X[i])

        for i in range(M):
            if i == 0:
                if X[i] != T[i]:
                    print(-1)
                    return
                X[i] = T[i]
            else:
                if X[i * LMC//M] is not None and X[i * LMC//M] != T[i]:
                    # for j in range(LMC):
                    #     print(j, X[j])
                    print(-1)
                    return
                # if X[i * LMC//M] is not None:
                #     print(i, i * LMC//M)
                X[i * LMC//M] = T[i]

        # # print(*X)
        print(LMC)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


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


def coprime(a, b):
    return gcd(a, b) == 1


def main():
    N, M = MI()
    S = input()
    T = input()

    LMC = lcm(N, M)

    if coprime(N, M):
        if S[0] == T[0]:
            print(LMC)
        else:
            print(-1)
    else:
        X = [None] * LMC
        for i in range(N):
            if i == 0:
                X[i] = S[i]
            else:
                X[i * LMC//N] = S[i]

        # for i in range(LMC):
        #     print(i, X[i])

        for i in range(M):
            if i == 0:
                if X[i] != T[i]:
                    print(-1)
                    return
                X[i] = T[i]
            else:
                if X[i * LMC//M] is not None and X[i * LMC//M] != T[i]:
                    # for j in range(LMC):
                    #     print(j, X[j])
                    print(-1)
                    return
                if X[i * LMC//M] is not None:
                    print(i, i * LMC//M)
                X[i * LMC//M] = T[i]

        # # print(*X)
        print(LMC)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations
import sys
import bisect
import string
import math
import time
from fractions import gcd
#import random


def I(): return int(input())


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['Yes', 'No']
MOD = 10**9+7
inf = float('inf')
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)


# show_flg = False
show_flg = True


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


def main():
    N, M = MI()
    S = input()
    T = input()

    LMC = lcm(N, M)
    X = [None] * LMC

    for i in range(N):
        if i == 0:
            X[i] = S[i]
        else:
            X[i * LMC//N] = S[i]

    # for i in range(LMC):
    #     print(i, X[i])

    for i in range(M):
        if i == 0:
            if X[i] != T[i]:
                print(-1)
                return
            X[i] = T[i]
        else:
            if X[i * LMC//M] is not None and X[i * LMC//M] != T[i]:
                # for j in range(LMC):
                #     print(j, X[j])
                print(-1)
                return
            X[i * LMC//M] = T[i]

    # print(*X)
    print(LMC)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import fractions
n,m=map(int,input().split())
s=str(input())
t=str(input())
f=fractions.gcd(n,m)
f2=n*m//f
#print(f2)
for i in range(f2,n*m+1,f2):
    z2=(i//n)*(i//m)//fractions.gcd(i//n,i//m)
    #print(i,z2)
    for j in range(1,i+1,z2):
        if s[j//(i//n)]!=t[j//(i//m)]:
            break
    else:
        print(i)
        break
else:
    print(-1)

Submission status: Runtime Error

Solution code:
rom fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p #20ms
tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)