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 math

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

cnt = 1
ns = 1
nt = 1
ss = 0
tt = 0
fail = False

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

ll = lcm(n,m)
l = ll

while l <= n * m:
    fail = False
    while not( ns >= l or nt >= l):
        while not(ns <= nt):
            nt += l / m 
            tt += 1

        if(ns == nt):
            if(s[ss] != t[tt]):
                fail = True
                break

        ns += l / n
        ss += 1
    if not(fail): 
        break
    cnt += 1
    l = ll * cnt

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

Submission status: Runtime Error

Solution code:


#最小公倍数
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 x//b

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

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:
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:
n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
ans = 0
if saisyou == n or saisyou == m:
    print(-1)
else:
    for l in range(saisyou,100000,saisyou):
        num = 0
        xlis = []
        ylis = []
        for i in range(min(n,m)):
            xlis.append(1+(l/n)*i+1)
            ylis.append(1+(l/m)*i+1)
        yset = set(ylis)
        xset = set(xlis)
        for i in yset:
            hanntei = 0
            for j in xset:
                if i == j:
                    if s[j] == t[i]:
                        break
                    else:
                        hanntei += 1
                        break
            if hanntei >= 1:
                break
        num += 1
        ans = l
        break 
    if num == 1:
        print(ans)    
    else:
        print(-1)

Submission status: Runtime Error

Solution code:
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
    left = 0
    right = big - 1
    for j in range(small):
        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

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

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)
x = ['']*l
c = ['']*l
d = ['']*l

c[0] = s[0]
for i in range(1,n):
    c[i*(l//n)] = s[i]

d[0] = t[0]
for i in range(1,m):
    d[i*(l//m)] = t[i]

ans = -1

for i in range(l):
    if l ==n or l ==m:
        ans = -1
        break
    elif c[i] == '' or d[i] == '':
        ans = l
        continue
    elif c[i] == d[i]:
        ans = l
        continue
    else:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

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


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

S = input()
T = input()

x = N * M // gcd(N,M)
flag = True

#print(S[0 * (N / math.gcd(N,M)) + 1])
for i in range(gcd(N,M)):
  if not(S[i * (N //gcd(N,M))] == T[i * (M // gcd(N,M))]):
    flag = False
#    print(S[i * (N // math.gcd(N,M))]  , T[i * (M // math.gcd(N,M))])
if flag:
  print(x)
else:
  print('-1')

Submission status: Runtime Error

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

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

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

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

if max(n, m) % min(n, m) != 0:
    print(l if s[0] == t[0] else -1)
    exit()

for i in range(0, min(n, m)):
    if s[i+max(n, m)//min(n, m)]:
        if s[i+max(n, m)//min(n, m)] != t[i]:
            print(-1)
            exit()

Submission status: Runtime Error

Solution code:
import math

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

S = input()
T = input()

x = N * M // math.gcd(N,M)
flag = True

#print(S[0 * (N / math.gcd(N,M)) + 1])
for i in range(math.gcd(N,M)):
  if not(S[i * (N // math.gcd(N,M))] == T[i * (M // math.gcd(N,M))]):
    flag = False
#    print(S[i * (N // math.gcd(N,M))]  , T[i * (M // math.gcd(N,M))])
if flag:
  print(x)
else:
  print('-1')

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

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

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

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

if N > M:
    a, b = N, M
    A, B = S, T
else:
    a, b = M, N
    A, B = T, S

if a % b == 0:
    for i in range(a):
        if A[int(i * a / b)] == B[i]:
            continue
        else:
            ans = -1
            break
    ans = (a - 1) / a * np.lcm(a, b) + 1
            
else:
    if A[0] == B[0]:
        ans = np.lcm(a, b)
    else:
        ans = -1        
        
print(ans)

Submission status: Runtime Error

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

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

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

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

if max(n, m) % min(n, m) != 0:
    print(l if s[0] == t[0] else -1)
    exit()

for i in range(0, min(n, m)):
    if s[i+max(n, m)//min(n, m)] != t[i]:
        print(-1)
        exit()

Submission status: Runtime Error

Solution code:
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)
LparN = L / N
LparM = L / M

listWord = [""] * L

flag = 0

for i in range(N):
  indexN = i * LparN + 1
  if indexN < L:
    listWord[int(indexN)] = S[i]
  else:
    flag = 1
for j in range(M):
  indexM = j * LparM + 1
  if indexM < L:
    if len(list(listWord[int(indexM)])) != 0:
      if listWord[int(indexM)] != T[j]:
        flag = 1
        break
    else:
      listWord[int(indexM)] = T[j]
  else:
    flag = 1
        
if flag == 0:
  print(L)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import sys,fractions;
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def S(): return input()

a,b = Is()
s,t = S(),S()
lcm = (a * b) // fractions.gcd(a, b)
x = ["."]*lcm
for i in range(a):
    x[i*(lcm//a)] = s[i]
for i in range(b):
    if x[i*(lcm//b)] == "." or x[i*(lcm//b)] == t[i]:
        continue
    else:
        print(-1)
        exit()
print(lcm)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = str(input())
t = str(input())
a = 0
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
ans = 0
if saisyou == n or saisyou == m:
    print(-1)
else:
    for l in range(saisyou,100000,saisyou):
        num = 0
        xlis = []
        ylis = []
        for i in range(min(n,m)):
            xlis.append(1+(l/n)*i+1)
            ylis.append(1+(l/m)*i+1)
        yset = set(ylis)
        xset = set(xlis)
        renketu = set(yset + xset)
        if len(yset) + len(xset) == renketu:
            ans = l
            break
    print(ans)

Submission status: Runtime Error

Solution code:
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
    left = 0
    right = big - 1
    for j in range(small):
        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
            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

Submission status: Runtime Error

Solution code:
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)
LparN = L / N
LparM = L / M

listWord = [""] * L

flag = 0

for i in range(N):
  indexN = i * LparN + 1
  if indexN < L:
    listWord[int(indexN)]  += S[i]
  else:
    flag = 1
for j in range(M):
  indexM = j * LparM + 1
  if indexM < L:
    if len(list(listWord[int(indexM)])) != 0:
      if listWord[int(indexM)] != T[j]:
        flag = 1
        break
    else:
      listWord[int(indexM)] += T[j]
  else:
    flag = 1
        
if flag == 0:
  print(L)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
import fractions
import sys

N,M=map(int,input().split())
S=input()
T=input()
S_len=len(S)
T_len=len(T)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

lcm_result=lcm(S_len,T_len)
S_data=[0]*lcm_result
count=0
for i in range (0,lcm_result,int(lcm_result/N)):
    S_data[i]=(S[count])
    count+=1

T_data=[0]*lcm_result
count=0
for i in range (0,lcm_result,int(lcm_result/M)):
    T_data[i]=(T[count])
    count+=1

if S_data[0]!=T_data[0]:
    print(-1)
    sys.exit()

for j in range (lcm_result):
    if S_data[j]!=0 and T_data[j]!=0 and S_data[j]!=T_data[j]:
        print (-1)
        sys.exit()
print(lcm_result)

Submission status: Runtime Error

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

x=1

gcd=fractions.gcd(M,N)

if gcd==0:
  print(-1)
  sys.exit()

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

lst=[S[0]]


lcm=(M*N)//gcd
dl=lcm//N
for i in range(1,N):
  lst+=['-']*(dl-1)+[S[i]]

dl=lcm//M
for i in range(1,M):

  if len(lst)<(i*dl):
    lst+=['-']*(i*dl-len(lst))
  if lst[i*dl] == '-' or lst[i*dl] == T[i]:
    lst[i*dl]=T[i]
  else:
    print(-1)
    sys.exit()
print(lcm)

Submission status: Runtime Error

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

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

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

if len(s) < len(t):
    tmp = s
    s = t
    t = tmp

for i in range(1, min(n, m)):
    if s[i+l//min(n, m)] != t[i]:
        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)

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

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(len(S))]
    t = [(i * ans)//M + 1 for i in range(len(T))]

    pool = s + t

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

    for st in double_st:
        s_i = my_index(s, st, -1)
        t_i = my_index(t, st, -1)
        if s_i == -1 or t_i == -1 or s_i >= len(S) or t_i >= len(T):
            continue
        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:
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
    left = 0
    right = big - 1
    for j in range(small):
        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
            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

Submission status: Runtime Error

Solution code:
import fractions
nm=raw_input()
nm=nm.split()
n=eval(nm[0])
m=eval(nm[1])
s=raw_input()
t=raw_input()

gg=fractions.gcd(n,m)

result = ll
for i in xrange(n):
    if not (i*m) % n :
            tmp=i*m/n
            if s[i] != t[tmp] :
                result= -1
print result

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)
x = ['']*l
c = ['']*l
d = ['']*l

c[0] = s[0]
for i in range(1,n):
    c[i*(l//n)] = s[i-1]

d[0] = t[0]
for i in range(1,m):
    d[i*(l//n)] = t[i-1]

ans = -1

for i in range(l):
    if c[i] == '' or d[i] == '':
        ans = l
        continue
    elif c[i] == d[i]:
        ans = l
        continue
    elif l ==n or l ==m:
        ans = -1
        break
    else:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

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

    for i in range(lcm(n, m)+1):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

x=1

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

lst=[S[0]]


lcm=(M*N)//fractions.gcd(M,N)
dl=lcm//N
for i in range(1,N):
  lst+=['-']*(dl-1)+[S[i]]

dl=lcm//M
for i in range(1,M):

  if len(lst)<(i*dl):
    lst+=['-']*(i*dl-len(lst))
  if lst[i*dl] == '-' or lst[i*dl] == T[i]:
    lst[i*dl]=T[i]
  else:
    print(-1)
    sys.exit()
print(lcm)

Submission status: Runtime Error

Solution code:
import sys
import math
import time

t_start = time.time()


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

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

if S[0] != T[0]:
    print(-1)
    sys.exit()
lcm_NM = lcm(N, M)

loop_flg = True
i = 0
while loop_flg==True:
    i +=1
    tmp_L1 = (i-1)*lcm_NM
    tmp_L = i*lcm_NM
    
    xn_index = [0]*(N-1)
    xm_index = [0]*(M-1)  
    for n in range(1, N):
        xn_index[n-1] = n*tmp_L//N+1
    for m in range(1, M):
        xm_index[m-1] = m*tmp_L//M+1
    n_set = set(xn_index)
    m_set = set(xm_index)
    matched_list = list(n_set.intersection(m_set))
    if len(matched_list) == 0:
        break
    else:
        for k in matched_list:
            if S[(k-1)*N//tmp_L] == T[(k-1)*M//tmp_L]:
                loop_flg = False
                continue
            else:
                t_elpsd = time.time() - t_start
                if t_elpsd > 1.9: 
                    print(-1)
                    sys.exit()
                else:
                    loop_flg = True
                    break
                    
L = tmp_L
print(L)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
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] * 100000
    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()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import fractions
import sys

N,M=map(int,input().split())
S=input()
T=input()
S_len=len(S)
T_len=len(T)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

lcm_result=lcm(S_len,T_len)
S_data=[0]*lcm_result
count=0
for i in range (0,lcm_result,int(lcm_result/N)):
    S_data[i]=(S[count])
    count+=1

T_data=[0]*lcm_result
count=0
for i in range (0,lcm_result,int(lcm_result/M)):
    T_data[i]=(T[count])
    count+=1

if S_data[0]!=T_data[0]:
    print(-1)
    sys.exit()

for j in range (fractions.gcd(S_len, T_len)-1,lcm_result,fractions.gcd(S_len, T_len)):
    if S_data[j]!=0 and T_data[j]!=0 and S_data[j]!=T_data[j]:
        print (-1)
        sys.exit()
print(lcm_result)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import math
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

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

    for i in range(lcm(n, m)+1):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

l = n*m//fractions.gcd(n,m)
x = ['']*l
c = ['']*l
d = ['']*l

c[0] = s[0]
for i in range(1,n):
    c[i*(l//n)] = s[i-1]

d[0] = t[0]
for i in range(1,m):
    d[i*(l//n)] = t[i-1]

ans = -1

for i in range(l):
    if c[i] == '' or d[i] == '':
        ans = l
        continue
    elif c[i] == d[i]:
        ans = l
        continue
    elif l ==n or l ==m:
        ans = -1
        break
    else:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
import math
def main():
    n, m = list(map(int, input().split(" ")))
    s = input()
    t = input()

    if n < m:
        n, m = m, n
        s, t = t, s

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

    for i in range(lcm(n, m)+1):
        if i == 0:
            continue
        if i % n != 0 or i % m != 0:
            continue

        s_spliters = list(range(0, i, i // n))
        t_spliters = list(range(0, i, i // m))

        flag = True
        count = 0
        for t_spliter in t_spliters:
            if t_spliter in s_spliters:
                index = s_spliters.index(t_spliter)
                if s[index] != t[count]:
                    flag = False
                    break
            count += 1

        if flag:
            print(i)
            return
    print(-1)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

l = n*m//math.gcd(n,m)
x = ['']*l
c = ['']*l
d = ['']*l

c[0] = s[0]
for i in range(1,n):
    c[i*(l//n)] = s[i-1]

d[0] = t[0]
for i in range(1,m):
    d[i*(l//n)] = t[i-1]

ans = -1

for i in range(l):
    if c[i] == '' or d[i] == '':
        ans = l
        continue
    elif c[i] == d[i]:
        ans = l
        continue
    elif l ==n or l ==m:
        ans = -1
    else:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
N, M = [int(x) for x in input().split(" ")]
S = input()
T = input()

N = len(S)
M = len(T)

import sys
sys.setrecursionlimit(5000000)


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


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


#print(gcd(N, M))
#print(maxkobai(N, M))


L = maxkobai(N, M)
ret = L
#x = ['-' for i in range(L+1)]
x = ['-'] * (L+1)

for n in range(0, N):
    x[n * (L//N) + 1] = S[n]

# print(x)

for m in range(0, M):
    idx = m * (L // M) + 1
    x_idx = x[idx]
    if x_idx != '-' and x_idx != T[m]:
        ret = -1
        break

print(ret)

Submission status: Runtime Error

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

l = n*m//fractions.gcd(n,m)
x = ['']*l
c = ['']*l
d = ['']*l

c[0] = s[0]
for i in range(1,n):
    c[i*(l//n)] = s[i-1]

d[0] = t[0]
for i in range(1,m):
    d[i*(l//n)] = t[i-1]

ans = -1

for i in range(l):
    if c[i] == '' or d[i] == '':
        ans = l
        continue
    elif c[i] == d[i]:
        ans = l
        continue
    elif l ==n or l ==m:
        ans = -1
    else:
        ans = -1
        break

print(ans)

Submission status: Runtime Error

Solution code:
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] * L
    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()

Submission status: Runtime Error

Solution code:
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)
LparN = L / N
LparM = L / M

listWord = [""] * L

flag = 0

for i in range(N):
  indexN = i * LparN + 1
  if indexN < L:
    listWord[int(indexN)]  += S[i]
  else:
    flag = 1
for j in range(M):
  indexM = j * LparM + 1
  if indexM < L:
    listWord[int(indexM)] += T[j]
  else:
    flag = 1
if flag == 0:
  for k in range(len(listWord)):
    check = list(listWord[k])
    if len(check) == 2:
      if check[0] != check[1]:
        flag = 1
        break
        
if flag == 0:
  print(L)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = str(input())
t = str(input())
if n > m:
    bigg = n
    summoo = m
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
unti = max(saisyou,n,m)
if unti%saisyou != 0:
    unti = saisyou((unti//saisyou)*saisyou + saisyou)
ans = 0
if bigg%summoo != 0:
    for l in range(unti,100000,saisyou):
        num = 0
        for i in range(min(n,m)):
            if 1+(l/n)*i+1 == 1+(l/m)*i+1:
                if s[i] == t[i]:
                    pass
                else:
                    num += 1
                    break
        if num == 0:
            ans = l
            break
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
N, M = [int(x) for x in input().split(" ")]
S = input()
T = input()

N = len(S)
M = len(T)

import sys
sys.setrecursionlimit(5000000)


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


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


#print(gcd(N, M))
#print(maxkobai(N, M))


L = maxkobai(N, M)
ret = L
#x = ['-' for i in range(L+1)]
x = ['-'] * (L+1)

for n in range(0, N):
    x[n * (L//N) + 1] = S[n]

# print(x)

for m in range(0, M):
    idx = m * (L // M) + 1
    x_idx = x[idx]
    if x_idx != '-' and x_idx != T[m]:
        ret = -1
        break

print(ret)

Submission status: Runtime Error

Solution code:
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)
LparN = L / N
LparM = L / M

listWord = [""] * L

flag = 0

for i in range(N):
  indexN = i * LparN + 1
  if indexN < L:
    listWord[int(indexN)]  += S[i]
  else:
    flag = 1
for j in range(M):
  indexM = j * LparM + 1
  if indexM < L:
    listWord[int(indexM)] += T[j]
  else:
    flag = 1
if flag == 0:
  for k in range(len(listWord)):
    check = list(listWord[k])
    if len(check) > 1:
      for check1 in range(len(check)):
        check2 = check1 + 1
        while check2 < len(check):
          if check[check1] != check[check2]:
            flag = 1
            break
          check2 += 1
        
if flag == 0:
  print(L)
else:
  print(-1)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = str(input())
t = str(input())
if n > m:
    bigg = n
    summoo = m
def get_gcd(a, b):
    if a < b:
        a, b = b, a
    if a % b == 0:
        return b
    else:
        return get_gcd(b, a % b)
def get_lcm(a, b, gcd):
    lcm = a * b // gcd
    return lcm
saisyou = get_lcm(n,m,get_gcd(n,m))
unti = max(saisyou,n,m)
unti = saisyou((unti//saisyou)*saisyou + saisyou)
ans = 0
if bigg%summoo != 0:
    for l in range(unti,100000,saisyou):
        num = 0
        for i in range(min(n,m)):
            if 1+(l/n)*i+1 == 1+(l/m)*i+1:
                if s[i] == t[i]:
                    pass
                else:
                    num += 1
                    break
        if num == 0:
            ans = l
            break
    print(ans)
else:
    print(-1)