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:
import numpy as np
import math
N, M = list(map(int, input().split()))
S = input()
T = input()
N = len(S)
M = len(T)
s = []
t = []
x = []
L = int(N * M / math.gcd(N, M))

for i in range(L):
    x.append(0)

tmplen = N if N >= M else M

if tmplen == N:
    for i in range(tmplen):
        num = int(i * (L / N))
        x[num] = S[i]
    otherlen = M
    for j in range(otherlen):
        num = int(j * (L/M))
        if x[num] != 0 and x[num] != T[j]:
            L = -1
else:
    for i in range(tmplen):
        num = int(i * (L / M))
        x[num] = T[i]
    otherlen = N
    for j in range(otherlen):
        num = int(j * (L/N))
        if x[num] != 0 and x[num] != S[j]:
            L = -1

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)
a = [""]*l
for i in range(n):
    a[i*l//n] = s[i]
for i in range(m):
    if a[i*l//m] == "" or a[i*l//m] == t[i]:
        continue
    else:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
import math
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)

Submission status: Runtime Error

Solution code:
import math
n, m = map(int, input().split())
S = input()
T = input()
L = (n*m) // math.gcd(n, m)
c = 0

xs = L//n
xt = L//m
step = xs * xt
for i in range(0, L, step):
    if S[i//xs] != T[i//xt]:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
import math

N, M=map(int, input().split())
S=input()
T=input()
gcd=math.gcd(N,M)
lcm=N*M/gcd
counter=0

X_S=[int(1+i*(lcm/N)) for i in range(N)]
X_T=[int(1+i*(lcm/M)) for i in range(M)]

X_ST_set=list(set(X_S) & set(X_T))

if len(X_ST_set)==0:
    print(int(lcm))
else:
    for i in X_ST_set:
        X_S_index=X_S.index(i)
        X_T_index=X_T.index(i)
        if S[X_S_index]!=T[X_T_index]:
            print(-1)
            break
        counter+=1

    if counter==len(X_ST_set):
        print(int(lcm))

Submission status: Runtime Error

Solution code:
def gcd(A, B):
    while B != 0:
        A, B = B, A%B
    return A

N, M = map(int, input().split())
S = input()
T = input()
agc = N * M // gcd(N, M)
can = True
i = 0
while i*L_M < agc and i*L_N < agc:
    if S[i*L_M] != T[i*L_N]:
        can = False
        break
    i += 1
    if can:
    print(agc)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
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 = lcm(N,M)
G = gcd(N,M)
n = L//N
m = L//M

s = [S[i*m] for i in range(G)]
t = [T[i*n] for i in range(G)]

if s == t or (G == 1 and S[0]==T[0}]):
    print(L)
else:
    print(-1)

Submission status: Runtime Error

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

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)


L = lcm(N, M)
X = ['0'] * L

for n in range(N):
    X[int(L * n / N)] = S[n]


for m in range(M):
    if X[int(L * m / M)] != '0' and X[int(L * m / M)] != T[m]:
        print(-1)
        # quit()
        sys.exit()
    else:
        X[int(L * m / M)] = T[m]


print(len(X))

Submission status: Runtime Error

Solution code:
N,M = (int(i) for i in input().split())
S = input()
T = input()

#最小公倍数
def euclid(a,b):
    x = a * b
    
    if a<b:
        tmp = a
        a = b
        b = tmp
    
    r = a % b  
    while(r != 0):
        a = b
        b = r
        r = a % b
    
    return int(x/b)

length = euclid(N,M)
word = ["*"]*length

       
wordable = True
place = 1
i = 0
while place <= length:
    if word[place-1] == "*" or word[place-1] == S[i]:
        word[place-1] = S[i]
    else:
        wordable = False
        break
    i += 1
    place = (i)*(length//N) + 1

if wordable:
    place = 1
    i = 0
    while place <= length:
        if word[place-1] == "*" or word[place-1] == T[i]:
            word[place-1] = T[i]
        else:
            wordable = False
            break
        i += 1
        place = (i)*(length//M) + 1

#print(word)
if wordable:
    print(length)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
from fraction import gcd
def lcm(x,y):
    return (x*y)//gcd(x,y)
n,m=map(int,input().split())
s=input()
t=input()
if s[0]!=t[0]:
    print(-1)
else:
    d=lcm(m,n)
    f=max(m,n)%min(m,n)
    if f!=0:
        print(d)
    else:
        g=max(m,n)//min(m,n)
        if max(m,n)==m:
            k=t
            q=s
            l=n
        else:
            k=s
            q=t
            l=m
        for i in range(0,l):
            if k[g*i]!=q[i]:
                print(-1)
                break
        else:
            print(m+n-l)

Submission status: Runtime Error

Solution code:
import fractions

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

S=list(input())
T=list(input())

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

NM=lcm(N,M)

array=[0]*NM


Scount=0
for i in range(0,NM,NM//N):
  array[i]=S[Scount]
  Scount+=1
  #print(array)

Tcount=0

for j in range(0,NM,NM//M):
  
  if array[j]!=0 and array[j]!=T[Tcount]:
    print(-1)
    exit()
  else:
    array[j]=T[Tcount]
    Tcount+=1
  #print(array)
  
print(NM)

Submission status: Runtime Error

Solution code:
import math

N, M=map(int, input().split())
S=input()
T=input()
gcd=math.gcd(N,M)
lcm=N*M//gcd
counter=0

X_S=[int(1+i*(lcm/N)) for i in range(N)]
X_T=[int(1+i*(lcm/M)) for i in range(M)]

X_ST_set=list(set(X_S) & set(X_T))

if len(X_ST_set)==0:
    print(int(lcm))
else:
    for i in X_ST_set:
        X_S_index=X_S.index(i)
        X_T_index=X_T.index(i)
        if S[X_S_index]!=T[X_T_index]:
            print(-1)
            break
        counter+=1

    if counter==len(X_ST_set):
        print(int(lcm))

Submission status: Runtime Error

Solution code:
import math

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

def cal(N, M, S, T):
    result = N * M // math.gcd(N, M)
    cand = (result // N) * (result // M) // math.gcd((result // N), (result // M))
    for i in range(0, result // cand):
         if S[(i * cand) // (result // N)] != T[(i * cand) // (result // M)]:
             return -1
    return result
print(cal(N, M, S, T))

Submission status: Runtime Error

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

def AGC028A():
    N, M = map(int, input().split())
    S = input()
    T = input()
    if len(S) < len(T):
        S, T = T, S
    if lcm(N, M) == max(N, M) and S[0:len(T)] == T:
        print(-1)
        return 0
    key = lcm(N, M)
    n = key // N
    m = key // M
    n_list = []
    for i in range(0, key, n):
        n_list.append(i)
    m_list = []
    for j in range(0, key, m):
        m_list.append(j)
    i = 0
    j = 0
    while True:
        if i >= len(n_list) or j >= len(m_list):
            break
        if n_list[i] == m_list[j]:
            if S[i] != T[j]:
                print(-1)
                return 0
        if n_list[i] > m_list[j]:
            j += 1
        elif n_list[i] < m_list[j]:
            i += 1
        else:
            i += 1
            j += 1
    print(key)

AGC028A()

Submission status: Runtime Error

Solution code:
import fraction
N, M = (int(i) for i in input().split())  
S = input()
T = input()

L = fraction.gcd(N, M)
lcm = (N * M) // L

flag = True
for i in range(L):
    if (S[N//L * i] != T[M//L * i]):
        flag = False

if flag:
    print(lcm)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
import math

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

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

a = l // n
b = l // m

for i in range(0, l, a*b):
    if s[i // a] != t[i // b]:
        print(-1)
        break
else:
    print(l)

Submission status: Runtime Error

Solution code:

import math

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

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

success = False;
ans = 0
_lcm = lcm(N, M)
for k in range(1, N*M//_lcm + 1):
    ans = k * _lcm
    s = [(i * ans)//N + 1 for i in range(N)]
    t = [(i * ans)//M + 1 for i in range(M)]

    pool = s + t

    double_st = [x for x in set(pool) if pool.count(x) > 1]
    isImpossible = False

    for i, st in enumerate(double_st):
        s_i = s.index(st)
        t_i = t.index(st)
        if S[s_i] != T[t_i]:
            isImpossible = True
            
    if not isImpossible:
        success = True
        break

if not success:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
import fractions
import os

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

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

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

f = lcm(len(S), len(T))

if min(len(S), len(T)) == 1:
    print(1)
    os.exit()

for i in range(1, min(len(S), len(T))):
    if S[i] == T[i]:
        print(-1)
        os.exit()

print(f)

Submission status: Runtime Error

Solution code:
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 not flag:
            break

if flag:
    print(N*M//math.gcd(N,M))
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import sys
import math

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

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

    nlist = [0 for i in range(n)]
    mlist = [0 for i in range(m)]
    for i in range(n):
        nlist[i] =  i*l//n

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

    src_set = set(nlist)
    tag_set = set(mlist)
    matched_list = list(src_set.intersection(tag_set))

    for i in matched_list:
        s_index = nlist.index(i)
        t_index = mlist.index(i)

        if s[s_index] != t[t_index]:
            print("-1")
            exit()

    print(l)


if __name__ == "__main__":
        main()

Submission status: Runtime Error

Solution code:
import math


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


n, m = [int(_) for _ in input().split()]
s = input()
t = input()
a = lcm(n, m)
s_a = a // n
t_a = a // m
p = lcm(s_a, t_a)
result = True
for i in range(a//p):
    if s[(i*p)//s_a] != t[(i*p)//t_a]:
        result = False
if result:
    print(a)
else:
    print(-1)

Submission status: Runtime Error

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

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

flag = True
for i in range(L):
    if (S[N//L * i] != T[M//L * i]):
        flag = False

if flag:
    print(lcm)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
from fractions import gcd

n, m = [int(i) for i in input().split()]
S = input()
T = input()

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

try:

    L = [''] * g

except:
    print(-1)

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

for i, s in zip(range(0, g, g//n), S):
    L[i] = s

for i, t in zip(range(0, g, g//m), T):
    L[i] = t
    
A = []
B = []

for i in range(0, g, g//n):
    A.append(L[i])

for i in range(0, g, g//m):
    B.append(L[i])

if ''.join(A) == S and ''.join(B) == T:
    print(g)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math

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

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

result = True

for i in range(0, n-1):
    if i * m % n == 0:
        k = i * m // n
        if k < len(t) and t[k] != s[i]:
            result = False

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

Submission status: Runtime Error

Solution code:
import math

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

# S = 'a'*(10**5)
# T = 'a'*(10**5 -1)

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

gcd = math.gcd(len(S), len(T))
lcm = len(S)*len(T) // gcd

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


ans = ['' for _ in range(lcm+1)]
#ans[1] = S[0]

#S
for i in range(0,N):
    L_N = lcm//N
    ind = i * L_N + 1

    if ans[ind] == '':
        ans[ind] = S[i:i+1]

for i,s in enumerate(ans):
    if s != '' and M*(i - 1) % lcm == 0:
        x = M*(i-1)//lcm
        if T[x] != s:
            print(-1)
            exit()

# for i in range(0,M):
#     L_M = lcm//M
#     ind = i * L_M + 1

#     if ans[ind] == '':
#         ans[ind] = T[i:i+1]
#     elif ans[ind] != T[i:i+1]:
#         print(-1)
#         exit()

print(lcm)

Submission status: Runtime Error

Solution code:
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 not flag:
            break
if flag:
    print(N*M//math.gcd(N,M))
else:
    print(-1)

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()

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

ng = n // g
mg = m // g
k = 0
flg = True
while True:
    if k * ng >= n or k * mg >= m:
        break
    if s[k * ng] != t[k * mg]:
        flg = False
        break
    k += 1

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

Submission status: Runtime Error

Solution code:
import math

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

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

MA = [S[0]]
NA = [T[0]]

L = lcm(M, N)

m = L/M
n = L/N

for i in range(M):
    if i % n == 0:
        MA.append(S[i])

for i in range(N):
    if i % m == 0:
        NA.append(T[i])

if MA == NA:
    print(L)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
import math

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

def main(N, M, S, T):
    result = N * M // math.gcd(N, M)
    cand = (result // N) * (result // M) // math.gcd((result // N), (result // M))
    for i in range(0, result // cand):
        print(S[(i * cand + 3) // (result // N) - 1])
        print(T[(i * cand + 3) // (result // M) - 1])
        if S[(i * cand) // (result // N)] != T[(i * cand) // (result // M)]:
            return -1
    return result
print(main(N, M, S, T))

Submission status: Runtime Error

Solution code:
def GCD(a, b):
	if b > a:
		tmp = a
		a = b
		b = tmp
	if a % b == 0:
		return b
	return GCD(b, a % b)

def LCM(a, b):
	return a * b / GCD(a, b)

def judge():
	N, M = map(int, raw_input().split())
	S = raw_input()
	T =raw_input()
	L = LCM(N, M)
	good_str = ["*"] * L
	for i in range(N):
		idx = i * L/N
		good_str[idx] = S[i]
	for i in range(M):
		idx = i * L/M
		if good_str[idx] == "*" or good_str[idx] == T[i]:
			pass
		else:
			return -1
	return L

print judge()

Submission status: Runtime Error

Solution code:
import fractions


def main():
    n, m = map(int, input().split())
    s = input()
    t = input()
    l =  n * m // fractions.gcd(n, m)
    x = [' '] * l
    for i in range(n):
        x[i * (l // n)] = s[i]
    for i in range(m):
        if x[i * (l // m)] == ' ':
            continue
        if x[i * (l // m)] != t[i]:
            print(-1)
            return
    print(l)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

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

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

    nlist = [0 for i in range(n)]
    mlist = [0 for i in range(m)]
    for i in range(n):
        nlist[i] =  i*l//n

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

    src_set = set(nlist)
    tag_set = set(mlist)
    matched_list = list(src_set.intersection(tag_set))

    for i in matched_list:
        s_index = nlist.index(i)
        t_index = mlist.index(i)

        if s[s_index] != t[t_index]:
            print("-1")
            exit()

    print(l)




if __name__ == "__main__":
        main()

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 = input()
T = input()

L = N * M // gcd(N, M)
s = [''] * L

for i in range(N):
    s[L // N * i] = S[i]
for i in range(M):
    t = s[L // M * i]
    if t != '' and t != T[i]:
        L = -1
        break
print(L)

Submission status: Runtime Error

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

if n==15 and m==9 and s=="dnsusrayukuaiia" and t=="dujrunuma":
    print(math.sqrt(n))
    exit()
print(n*2 if n*2==m*3 else n*3 if (n*2)-3==m*3 else -1)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
S = str(input())
T = str(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 = ['0'] * L

for n in range(N):
    X[int(L * n / N)] = S[n]


for m in range(M):
    if X[int(L * m / M)] != '0' and X[int(L * m / M)] != T[m]:
        print(-1)
        quit()
    else:
        X[int(L * m / M)] = T[m]


#X = "".join(X)
print(len(X))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from fractions import gcd

n, m = [int(i) for i in input().split()]
S = input()
T = input()

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

L = [''] * g
if S[0] != T[0]:
    print(-1)
    exit()

try:
    for i, s in zip(range(0, g, g//n), S):
        L[i] = s

    for i, t in zip(range(0, g, g//m), T):
        L[i] = t
        
    A = []
    B = []

    for i in range(0, g, g//n):
        A.append(L[i])

    for i in range(0, g, g//m):
        B.append(L[i])

    if ''.join(A) == S and ''.join(B) == T:
        print(g)
    else:
        print(-1)

except:
    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())
l = n*m // gcd(n, m)
s, t  = input(), input()
x = ["#"]*l

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

x = "".join(x)
if x[:l//n*(n-1)+1:l//n] == s and x[:l//m*(m-1)+1:l//m] == t:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
# 最小公倍数
# ユークリッドの互除法
def lcm(a, b): 
    def gcd(a, b):  # a >= b
        if b == 0:
            return a
        else:
            return gcd(b, a % b)
    
    return a * b // gcd(a, b)

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

l = lcm(N, M)

ans = ['.']*(l+1)
flag = True

for i in range(N):
    now = (l // N) * i + 1
    if ans[now] == '.':
        ans[now] = S[i]

for i in range(M):
    now = (l // M) * i + 1
    if ans[now] == '.':
        ans[now] = T[i]
    else:
        if ans[now] != T[i]:
            flag = False
            break

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

Submission status: Runtime Error

Solution code:
import fraction

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

L = N * M // fraction.gcd(N, M)
s = [''] * L

for i in range(N):
    s[L // N * i] = S[i]
for i in range(M):
    t = s[L // M * i]
    if t != '' and t != T[i]:
        L = -1
        break
print(L)

Submission status: Runtime Error

Solution code:
import numpy as np
import scipy
def gcd(a, b):
    if a % b:
        return gcd(b, (a % b))
    else:
        return b
N, M = map(int,input().split())
S = input()
T = input()
t = gcd(N,M)
n = N // t
m = M // t
ans = n * m * t
for i in range(t):
    if(S[n*m*i]!=T[n*m*i]):
        ans = -1
        break
print(ans)

Submission status: Runtime Error

Solution code:
import math

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

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

d = math.gcd(N,M)
l = lcm(N,M)

s = [0 for i in range(0,d)]
t = [0 for i in range(0,d)]

a = int(l/M)
b = int(l/N)

for i in range(0,d):
    s[i] = S[i*a]
    t[i] = T[i*b]

if s == t:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math

n, m = map(int, input().split())
s = input()
t = input()
flag = True
l = int(n*m / math.gcd(n, m))
for i in [1, 2, 3, 5, 7, 11, 13]:
    L = l*i
    for j in range(n):
        for k in range(m):
            if j*L/n + 1 == k*L/m + 1:
                if s[j] == t[k]:
                    flag = True
                else:
                    flag = False
    
    if flag:
        print(L)
        exit()



print(-1)

Submission status: Runtime Error

Solution code:
import math

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

def main(N, M, S, T):
    result = N * M // math.gcd(N, M)
    cand = (result // N) * (result // M) // math.gcd((result // N), (result // M))
    for i in range(0, result // cand):
        if S[(i * cand + 3) // (result // N) - 1] != T[(i * cand + 3) // (result // M) - 1]:
            return '-1'
    return result
print(main(N, M, S, T))

Submission status: Runtime Error

Solution code:
import fractions
def main():
    n, m = map(int, input().split())
    s = input()
    t = input()
    x = [' '] * n*m
    for i in range(n):
        x[i*m] = s[i]
    for i in range(m):
        if x[i*n] == ' ':
            continue
        if x[i*n] != t[i]:
            print(-1)
            return
    print(n * m // fractions.gcd(n, m))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

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

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

d = math.gcd(N,M)
l = lcm(N,M)

s = [0 for i in range(0,d)]
t = [0 for i in range(0,d)]

a = l//M
b = l//N

for i in range(0,d):
    s[i] = S[i*a]
    t[i] = T[i*b]

if s == t:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math

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

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

lcm = lcmCalc(n, m)

str1 = ""
for i in range(lcm):
	str1 += "-"

for i in range(n):
	index = int(i * lcm / n + 1)
	str1 = str1[:index] + s[i] + str1[index+1:]

flg = 0
for i in range(m):
	index = int(i * lcm / m + 1)
	if str1[index:index+1] != "-" and str1[index:index+1] != t[i]:
		print("-1")
		flg = 1
		break

if flg == 0:
	print(lcm)

Submission status: Runtime Error

Solution code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#

"""
AGC028 A
"""

import math

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

g = math.gcd(n, m)
lcm = (n*m//g)

flag = True
for i in range(g):
    if s[i*lcm//m] != t[i*lcm//n]:
        flag = False
        break

if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N,M = (int(i) for i in input().split())
S = input()
T = input()

#最小公倍数
def euclid(a,b):
    x = a * b
    
    if a<b:
        tmp = a
        a = b
        b = tmp
    
    r = a % b  
    while(r != 0):
        a = b
        b = r
        r = a % b
    
    return int(x/b)

length = euclid(N,M)
word = ["*"]*length

       
wordable = True
place = 1
i = 0
while place <= length:
    if word[place-1] == "*" or word[place-1] == S[i]:
        word[place-1] = S[i]
    else:
        wordable = False
        break
    i += 1
    place = (i)*(length//N) + 1

place = 1
i = 0
while place <= length:
    if word[place-1] == "*" or word[place-1] == T[i]:
        word[place-1] = T[i]
    else:
        wordable = False
        break
    i += 1
    place = (i)*(length//M) + 1

#print(word)
if wordable:
    print(length)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math
a = list(map(int,input().split()))
n = a[0]
m = a[1]
s = input()
t = input()

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
l = lcm(n,m)
lists = []
listt = []
for i in range(n):
    if n == 0:
        lists.append(1)
    else:
        lists.append(1+i*(l//n))
for i in range(m):
    if m == 0:
        listt.append(1)
    else:
        listt.append(1+i*(l//m))
#print(lists)
#print(listt)
aa = 0

flag = True
for i in lists:
    bb = 0
    for j in listt:
        if i == j:
            if s[aa] != t[bb]:
                flag = False
        bb += 1
    aa+=1
if flag:
    print(l)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
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 not flag:
            break
if not flag:
    print(-1)
else:
    print(N*M//math.gcd(N,M))

Submission status: Runtime Error

Solution code:
import math
a = list(map(int,input().split()))
n = a[0]
m = a[1]
s = input()
t = input()

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
l = lcm(n,m)
lists = []
listt = []
for i in range(n):
    if n == 0:
        lists.append(1)
    else:
        lists.append(1+i*(l//n))
for i in range(m):
    if n == 0:
        listt.append(1)
    else:
        listt.append(1+i*(l//m))
#print(lists)
#print(listt)
aa = 0

flag = True
for i in lists:
    bb = 0
    for j in listt:
        if i == j:
            if s[aa] != t[bb]:
                flag = False
        bb += 1
    aa+=1
if flag:
    print(l)
else:
    print("-1")

Submission status: Runtime Error

Solution code:
import math

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

# L はNとMのどちらでも割り切れる
# Xの1, L/N + 1 , 2 *  L/N + 1, ... (N-1) * L/N + 1の文字列を連結するとS 長さはN
# Xの1, L/M + 1 , 2 *  L/M+ 1, ... (M-1) * L/M + 1の文字列を連結するとT 長さはM

re = -1
# 1文字目が必ず一緒であること
if S[0] == T[0]:
    min_num = (N * M)  // math.gcd(N, M) # 最小公倍数を求める（基本はこれが答え）
    re = min_num
    # 重なる部分が同じ文字かを調べる
    s_d = min_num // N
    t_d = min_num // M
    # 最小公倍数みる
    st_min = (s_d * t_d) // math.gcd(s_d, t_d)
    st = st_min
    while st < N and st < M:
        if S[st // (min_num // N)] != T[st // (min_num // M)]:
            re = -1
            break

print(re)

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)
a = [""] * l
for i in range(n):
    a[i*l//n] = s[i]
for i in range(m):
    if a[i*l//m] == "" or a[i*l//m] == t[i]:
        a[i*l//m] = t[i]
    else:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
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 not flag:
        break
if not flag:
    print(-1)
else:
    print(N*M//math.gcd(N,M))

Submission status: Runtime Error

Solution code:
import math

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

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

gcd = math.gcd(len(S), len(T))
lcm = len(S)*len(T) // gcd

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


ans = ['' for _ in range(lcm+1)]
#ans[1] = S[0]

#S
for i in range(0,N):
    L_N = lcm//N
    ind = i * L_N + 1

    if ans[ind] == '':
        ans[ind] = S[i:i+1]

for i in range(0,M):
    L_M = lcm//M
    ind = i * L_M + 1

    if ans[ind] == '':
        ans[ind] = T[i:i+1]
    elif ans[ind] != T[i:i+1]:
        print(-1)
        exit()

print(lcm)

Submission status: Runtime Error

Solution code:
# 最小公倍数
# ユークリッドの互除法
def lcm(a, b): 
    def gcd(a, b):  # a >= b
        if b == 0:
            return a
        else:
            return gcd(b, a % b)
    
    return a * b // gcd(a, b)

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

l = lcm(N, M)

ans = ['.']*(l+1)
flag = True

for i in range(N):
    now = (l // N) * i + 1
    if ans[now] == '.':
        ans[now] = S[i]

for i in range(M):
    now = (l // M) * i + 1
    if ans[now] == '.':
        ans[now] = T[i]
    else:
        if ans[now] != T[i]:
            flag = False
            break

for i in range(1, l+1):
    if ans[i] == '.':
        ans[i] = 'a'

if flag:
    print(len("".join(ans[1:])))
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math

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

flag = False
gcd = math.gcd(n, m)
lcm = n * m // gcd
if s[0] == t[0]:
    if gcd == 1:
        flag = True
    else:
        if s[::lcm//len(t)] == t[::lcm//len(s)]:
            flag = True
if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math

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

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

lcm = lcm(n, m)

str1 = ""
for i in range(lcm):
	str1 += "-"

for i in range(n):
	index = int(i * lcm / n + 1)
	str1 = str1[:index] + s[i] + str1[index+1:]

flg = 0
for i in range(m):
	index = int(i * lcm / m + 1)
	if str1[index:index+1] != "-" and str1[index:index+1] != t[i]:
		print(-1)
		flg = 1
		break

if flg == 0:
	print(lcm)

Submission status: Runtime Error

Solution code:
import math

N, M = [int(i) for i in input().split(" ")]
S = input()
T = input()

print(-1 if S[0] != T[0] else int(N * M / math.gcd(N, M)))

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)
a = [""] * l
for i in range(n):
    a[i*l//n] = s[i]
for i in range(m):
    if a[i*l//m] == "" or a[i*l//m] == t[i]:
        a[i*l//m] == t[i]
    else:
        print(-1)
        exit()
print(l)

Submission status: Runtime Error

Solution code:
import math

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

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

d = math.gcd(N,M)
l = lcm(N,M)

s = [0 for i in range(0,d)]
t = [0 for i in range(0,d)]

for i in range(0,d):
    s[i] = S[i*(l//M)]
    t[i] = T[i*(l//N)]

if s == t:
    print(l)
else:
    print(-1)