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:
Jupyter Notebook
AGC-n
Current Kernel Logo
Python 3 


1
#A
2
N=input()
3
A_list=list(map(int,input().split()))
4
n=0
5
for i in range(len(A_list)-1):
6
    if A_list[i]==A_list[i+1]:
7
        A_list[i+1]+=10000
8
        n+=1
9
print(n)
1
2 3 4 5
0

1
#B
2
import math
3
T=int(input())
4
for i in range(T):
5
    check=0
6
    A,B,C,D=map(int,input().split())
7
    A_list=[]
8
    if B>D:
9
        print('No')
10
        check=1
11
    while check==0:
12
        k=math.ceil((A-C)/B) if A>=C else 1
13
        A-=k*B
14
        if A<0:
15
            print('No')
16
            break
17
        elif A<=C:
18
            A+=D
19
            if (A in A_list or A==0):
20
                print('Yes')
21
                break
22
            A_list.append(A)
1
9 7 5 9
No

1
# B
2
import math
3
T=int(input())
4
abcd=list(map(int,input().split()))
5
for i in range(T):
6
    sugoi='no'
7
    A,B,C,D=abcd[4*i],abcd[4*i+1],abcd[4*i+2],abcd[4*i+3]
8
    A_list=[]
9
    if B>D:
10
        print('No')
11
        sugoi='yes'
12
    while sugoi=='no':
13
        k=math.ceil((A-C)/B) if A>=C else 1
14
        A-=k*B
15
        if A<0:
16
            print('No')
17
            break
18
        elif A<=C:
19
            A+=D
20
            if A in A_list:
21
                print('Yes')
22
                break
23
            A_list.append(A)

1
import random
2
moji=''
3
for i in range(96*4):
4
    k=random.randrange(10,100000)
5
    moji+=str(k)+' '
6
print(moji)
49577 38748 51545 14229 66123 10159 13594 6667 75070 86954 43840 53738 39847 89585 9933 10129 70224 43454 45481 51487 54133 32286 78378 6497 73223 12189 87999 65011 43165 32546 8432 49737 43518 87213 90515 92099 74897 55565 46842 11081 84008 30991 44856 10305 72212 79367 86422 23049 34681 16096 93431 10032 78460 20377 44252 84334 98591 30972 75860 61883 48522 6988 85488 44798 39847 51447 49578 24157 69296 30493 8039 86315 33193 24243 23835 60725 99843 15788 27649 78531 7473 45213 58255 19078 63934 67284 38890 39155 25554 68632 65336 41502 97236 2317 93279 31025 60928 37625 44284 90314 42272 998 13481 53773 89382 98693 89134 76034 66399 95908 83397 9050 1185 9287 13075 22268 32701 95365 39540 3229 98603 59523 94096 44043 57083 6753 46555 1918 42947 8303 64854 37648 61525 98387 86705 38499 91181 48418 50920 29372 58029 24455 90742 14303 9907 73692 25233 34412 44743 33412 92719 94350 35737 16182 49794 25665 53330 47138 92539 696 54581 93518 36745 80424 59149 22790 697 72606 34502 94277 218 20114 81501 41453 44708 75123 25623 6222 51387 2561 47426 416 92800 23639 15666 47122 73472 99766 44914 48566 56201 71348 18268 93876 46551 35164 72609 59316 90756 20705 40484 12132 24345 21801 95572 69432 55858 73229 79945 68822 32966 73732 69373 7177 10817 53863 30883 46980 73951 88471 27314 53286 60991 14070 5757 23970 42978 55057 77030 64451 94209 19825 98049 89573 99554 23004 91677 11626 73234 26402 83987 76501 48311 50527 27379 8455 38923 86818 20238 67077 34259 22630 20689 60414 25516 61921 62161 31533 26844 6818 15493 19052 66511 77893 29455 21509 85070 90188 67693 61281 24487 86300 19230 90598 94835 64004 18362 57697 60707 19909 84171 95392 34820 63054 22243 90465 35761 45148 71903 58818 78033 45587 73626 13049 61152 46111 75380 88825 87574 34409 37390 62366 4025 68652 64514 86326 7689 80484 69780 15598 9697 54332 37388 45287 73660 41083 75293 55400 86645 99173 40831 29754 2975 9435 66198 66581 19149 73914 41325 25900 66326 93100 26224 45245 51190 80973 69280 75152 84285 24035 97505 46366 43162 56726 20367 86063 98812 64109 33949 3112 52584 22431 87110 59764 81415 66427 14112 79314 59522 60695 30091 22120 51701 90949 33273 75703 88454 54028 42622 18003 27609 80241 95556 35884 61115 91784 97200 60096 86929 94001 84670 81425 85365 34807 

1
#C
2
from itertools import permutations
3
N=int(input())
4
S=input()
5
S0=S
6
ways=-1
7
"""moji='abcdefghijklmnopqrstuvwxyz'
8
for i in range(len(moji)):
9
    print(S,S.count(moji[i]))
10
    if S.count(moji[i])%2==1:
11
        ways=0
12
        break
13
print('check ok')"""
14
if ways!=0:
15
    ways=0
16
    nums=[0,]*(N-1)+[1,]*N
17
    color=list(set(permutations(nums,2*N-1)))
18
    color.sort()
19
    for j in range(len(color)):
20
        S=S0
21
        s1,s2='',''
22
        j_color=(0,)+color[j]
23
        for i in range(2*N):
24
            if j_color[i]==0:
25
                s1+=S[i]
26
                S=S[:i]+'0'+S[i+1:]
27
        s2=(S.replace('0',''))[::-1]
28
        print(j_color,s1,s2,s1==s2)
29
        if s1==s2:ways+=1
30
print(2*ways)
11
mippiisssisssiipsspiim

1
moji='abcdefg'
2
i=3
3
moji=moji[:i]+'0'+moji[i+1:]
4
moji
'abc0efg'

1
#027A
2
N,x=map(int,input().split())
3
children=list(map(int,input().split()))
4
children.sort()
5
fun=0 if x!=sum(children) else N
6
if fun==0:
7
    for i in range(N):
8
        x-=children[i]
9
        if x>=0:fun+=1
10
        else: break
11
    if fun==N:fun-=1
12
print(fun)
2 10
20 20
0

1
#027B
2
N,X=map(int,input().split())
3
gomi=list(map(int,input().split()))
4
​

-1
1
N,M=map(int,input().split())
2
S=input()
3
T=input()
4
​
5
def gcd(a,b):
6
    if a<b:a,b=b,a
7
    c=a%b
8
    while c>0:
9
        a,b=b,c
10
        c=a%b
11
    return b
12
​
13
def enstr(A):
14
    S=''
15
    for i in A:
16
        S+=i
17
    return S
18
    
19
if S[0]==T[0]:
20
    gcd=gcd(N,M)
21
    if N<M:
22
        N,M=M,N
23
        S,T=T,S
24
    if gcd==M:
25
        if S==T*(N//M):
26
            print(N)
27
        else:
28
            print('-1')
29
    else:
30
        gcm=N*M//gcd
31
        print(gcm)
32
print(-1)
3 2
a
c
-1

15 9
1
15 9

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import math

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

if not((S in T) or (T in S)):
    ans = int(N * M / math.gcd(N, M))
else:
    ans = -1

print(ans)

Submission status: Runtime Error

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

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

res = False
clear_all = True
for i in range(1,n+1):
    for j in range(1,m+1):
        if a * i - b * j == a - b:
            if not s[i-1] == t[j-1]:
                clear_all = False
if clear_all:
    res = True
if res:
    print(l)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

def a():
    N, M = map(int, input().split(' '))
    S = input()
    T = input()
    L = (N*M)//fractions.gcd(N, M)
    X = ['0']*L
    for i, s in enumerate(S):
        X[(L//N)*i] = s
    for i, t in enumerate(T):
        if X[(L//M)*i] == t or X[(L//M)*i] == '0':
            continue
        else:
            print(-1)
            return
    print(L)
    return

a()

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

a = lcm//n
b = lcm//m

flag=0
le = min(n,m)//(min(n,m)//gcd)

for i in range(le):
    # print(i*b, i*a)
    if s[i*b]!=t[i*a]:
        flag=1

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

Submission status: Runtime Error

Solution code:
from math import *
import numpy as np

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 = N
    A = S
    b = M
    B = T
else:
    a = M
    A = T
    b = N
    B = S

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

Submission status: Runtime Error

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

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

if N % M != 0 and M % N != 0:
    print(lcm(N,M))
elif N % M == 0:
    # a is the larger string
    step = N // M
    for i in range(M):
        if b[i] != a[i + step]:
            print(-1)
            quit()
    print(N)
else:
    # b is the larger string
    step = M // N
    for i in range(N):
        if a[i] != b[i + step]:
            print(-1)
            quit()
    print(M)

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


gcdNM = math.gcd(N, M)
lcmNM = lcm(N, M)

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

if isGood and T[0] == S[0]:
    print(lcmNM)
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 = list(map(int, input().split()))
S = list(input())
T = list(input())
flag = True

if S[0] != T[0]:
  flag = False

if N % M == 0 or M % N == 0:
  flag = False

if flag:
  print(lcm(N, M))
else:
  print(-1)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

import sys
import itertools
import math

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

    if N < M:
        t = M
        M = N
        N = t
        q = S
        S = T
        T = q

    d = math.gcd(N, M)

    n = N / d

    ret = -1
    for step in range(1, M+1):
        slen = step * N
        if slen % M != 0:
            continue
        mstep = slen // M
        good = True
        for i in range(N):
            u = (step * i) % mstep
            if u != 0:
                continue
            j = (step * i) // mstep
            if S[i] != T[j]:
                good = False
                break
        if good:
            ret = step * N
            break

    # 出力
    print("{}".format(ret))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N,M = list(map(int,input().split()))
S = input()
T = input()
sl = list(S)
tl = list(T)
import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

lcm = lcm(N,M)
Sd = lcm//N
St = lcm//M
kaisu = min((N-1)//St,(M-1)//Sd)
flag = 0
for i in range(kaisu+1):
    if sl[St*i] != tl[Sd*i]:
        flag = 1
if flag == 0:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math
n,m=map(int,input().split())
s=input()
t=input()
c=0
for i in range(m):
    for j in range(n):
        if m*j==n*i:
            if s[j]==t[i]:
                continue
            else:
                c+=1
                break
        else:
            continue
            
if c!=0:
    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()

for i in range(lcm):
    if N*i == M*i:
        if S[N*i] != T[M*i]:
            print(-1)
            exit()
print(lcm)

Submission status: Runtime Error

Solution code:
import math

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

if N==M:
    print(int(N*M)) if S==T else print(-1)
else:
    g = math.gcd(N, M)
    if g==1:
        print(int(N*M))
    else:
        flag = False
        s, t = N/g, M/g
        ss = list(map(lambda x: int(x*s), list(range(0,g))))
        tt = list(map(lambda x: int(x*t), list(range(0,g))))
        for i,j in zip(ss,tt):
            if S[i]!=T[j]:
                flag = True
        if flag:
            print(-1)
        else:
            print(int(s*t*g))

Submission status: Runtime Error

Solution code:
import math

input_lines = input().split()
N = int(input_lines[0])
M = int(input_lines[1])
S = input()
T = input()
#print(N,M,S[0],T[0])
L = int(N*M/math.gcd(N,M))
x = 1
count = 0
while True:
    k = L*x
    flag = 1
    for i in range(N):
        for j in range(M):
            if((i-1)*k/N+1 == (j-1)*k/M+1):
                if(S[i-1] != T[j-1]):
                    flag = 0
                    count += 1
                    break
    if(flag == 1):
        print(k)
        break
    if(count == 2):
        print(-1)
        break
    x += 1

Submission status: Runtime Error

Solution code:
import math
n,m=map(int,input().split())
s=input()
t=input()
cnt=1
i=0
flag=0

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

r=l/m

while cnt<n:
  if s[int(i*l/m)]==t[int(i*l/n)]:
    flag=1
    cnt+=r
    i+=1
  else:
    flag=0
    break

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

Submission status: Runtime Error

Solution code:
import math
def main():
    N, M = map(int, input().split())
    S = input()
    T = input()
    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(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())
l = n*m // gcd(n, m)
s, t  = input(), input()
if s[0] != t[0]:
    print(-1)
    exit()
if n > m:
    n, m = m, n
    s, t = t, s
x = ["#"]*l

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

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

Submission status: Runtime Error

Solution code:
import math

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

def cal(S,T,N,M):
    for i in range(N):
        for j in range(M):
            if(i*M==j*N):
                if(S[i]!=T[j]):
                    return -1
    return lcm(N,M)

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

Submission status: Runtime Error

Solution code:
import math
def main():
    N, M = map(int, input().split())
    S = input()
    T = input()
    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(main())

Submission status: Runtime Error

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

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

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

print(re)

Submission status: Runtime Error

Solution code:
# -*- coding:utf-8 -*-
import math

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

ans = int(0)

if not((S in T) or (T in S)):
    ans = int(N * M / math.gcd(N, M))
else:
    ans = -1

print(ans)

Submission status: Runtime Error

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

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

if S[0] != T[0]:
  flag = False

if N % M == 0 or M % N == 0:
  flag = False

if flag:
  print(lcm(N, M))
else:
  print(-1)

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                         

    lcm_ = lcm(n, m)                        

    for i in range(10000000000):            
        if (i % n != 0 or i % m != 0) or i == 0:                                         
            continue                        
        n_split = map(lambda x: x + 1, list(range(i // n)))                              
        m_split = map(lambda x: x + 1, list(range(i // m)))                              

        n_count = 0                         
        flag = False                        
        for j in n_split:                   
            m_count = 0                     
            for k in m_split:               
                if j < k:                   
                    break                   
                if j == k:                  
                    if s[n_count] != t[m_count]:                                         
                        flag = True         
                m_count += 1                
            n_count += 1                    
        if not flag:                        
            print(i)                        
            return                          
    print(-1)                               

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

if __name__ == "__main__":                  
    main()

Submission status: Runtime Error

Solution code:
import math
n, m = map(int, input().split())
listS = list(input())
listT = list(input())
listX = []

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

if listS[0]!=listT[0]:
    print(-1)
else:
    index =0
    index2=0
    while index < l:
        while index2 < n:
            if index == index2*a:
                listX.append(listS[index2])
                index2+=1
                index+=1
            else:
                listX.append(0)
                index+=1
        index+=1
    index4=0
    flag=0
    while index4 < min(m, l//b):
        if listX[index4*b] == listT[index4] or listX[index4*b] == 0:
            index4+=1
        else:
            print(-1)
            flag=1
            break
    if flag!=1:
        print(l)

Submission status: Runtime Error

Solution code:
import math
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) // math.gcd(x, y)
lcm_result=lcm(S_len,T_len)
S_data=[]
count=0
for i in range (lcm_result):
    if i == (lcm_result/N)*count:
        S_data.append(S[count])
        count+=1
    else:
        S_data.append(0)
T_data=[]
count=0
for i in range (lcm_result):
    if i == (lcm_result/M)*count:
        T_data.append(T[count])
        count+=1
    else:
        T_data.append(0)
for j in range (1,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 math

N, M = list(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 st in double_st:
        s_i = s.index(st, -1)
        t_i = t.index(st, -1)
        if s_i == -1 or t_i == -1:
            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:
import math

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


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


start = lcm(N, M)
ans = 0
for i in range(10000):
    L = (i+1) * start
    pos_dic = {}
    ng_flg = 0

    if L < (N - 1) * (L / N) + 1 or L < (M - 1) * (L / M) + 1:
        continue

    for j, s in enumerate(S):
        pos_dic[j * (L / N) + 1] = s
    for k, t in enumerate(T):
        if pos_dic.get(k * (L / M) + 1, None) is None:
            pass
        else:
            if pos_dic[k * (L / M) + 1] == t:
                pass
            else:
                ng_flg = 1
                break

    if ng_flg == 0:
        ans = L
        break

if ans == 0:
    print('-1')
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
S=input()
T=input()
import math
gcd=int(N*M/math.gcd(N,M))
lis=[int(gcd/N),int(gcd/M)]
for i in range(int(N*M/gcd)):
    if S[int(i*lis[1])]!=T[int(i*lis[0])]:
        print(-1)
        exit()
print(gcd)

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

    lcm_ = lcm(n, m)

    for i in range(10000000000):
        if (i % n != 0 or i % m != 0) or i == 0:
            continue
        n_split = map(lambda x: x + 1, list(range(i // n)))
        m_split = map(lambda x: x + 1, list(range(i // m)))

        n_count = 0
        flag = False
        for j in n_split:
            m_count = 0
            for k in m_split:
                if j < k:
                    break
                if j == k:
                    if s[n_count] != t[m_count]:
                        flag = True
                m_count += 1
            n_count += 1
        if not flag:
            print(i)
            return
    print(-1)

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

if __name__ == "__main__":
    main()

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 math

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

def cal(S,T,N,M):
    for i in range(N):
        for j in range(M):
            if(i/N==j/M):
                if(S[i]!=T[j]):
                    return -1
    return lcm(N,M)

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

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()
can = True
agc = N // gcd(N, M) * M
L_N = agc // N
L_M = agc // M
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:
# -*- coding: utf-8 -*-

import math


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


def divisors(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list


if __name__ == '__main__':
    N, M = map(int, input().split(' '))
    S = input()
    T = input()

    L = lcm(N, M)

    m = max(N, M)
    m_divisors = divisors(m)
    m_divisors.remove(1)

    S = S * int(math.ceil(m / N))
    T = T * int(math.ceil(m / M))

    for i in [d - 1 for d in m_divisors]:
        if S[i] == T[i]:
            print(-1)
            exit(0)
    print(L)

Submission status: Runtime Error

Solution code:
import math

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

g = math.gcd(n, m)
l = int(n * m / g)
if l != n and l != m:
    print(l)
else:
    if n > m:
        res = n
        for i in range(m):
            if s[i * int(n / m)] == t[i]:
                pass
            else:
                res = -1
                break
    else:
        res = m
        for i in range(n):
            if t[i * int(m / n)] == s[i]:
                pass
            else:
                res = -1
                break
    print(res)

Submission status: Runtime Error

Solution code:

import math

N, M = list(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 st in 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 math
import sys
n,m=map(int,input().split())
s=input()
t=input()
l = n*m / math.gcd(n,m)
slist=[1]
tlist=[1]
for i in range(1,n):
    j=i*(l/n)+1 
    jj=int(j)
    if j==jj:
        slist.append(j)
    k=i*(l/m) + 1
    kk=int(k)
    if k==kk:
        tlist.append(k)
slist=list(filter(lambda x: x <= l, slist))
tlist=list(filter(lambda x: x <= l, tlist))
se=[]
for i in range(int(l)):
    se.append(0)
for i in range(len(slist)):
    se[int(slist[i])-1] = s[i]
for i in range(len(tlist)):
    if se[int(tlist[i]) -1] != 0 and se[int(tlist[i]) -1] != t[i]:
        print(-1)
        sys.exit()
print(int(l))

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()
can = True
agc = N * M // gcd(N, M)
L_N = agc // N
L_M = agc // M
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:
import fraction
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

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

lcm_num = lcm(N,M)

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

# print(X_s,X_t)

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


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

# print(X_s,X_t)

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

print(lcm_num)

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

Submission status: Runtime Error

Solution code:
# from math import gcd
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 N > M:
  N, M = M, N
  S, T = T, S

i = 0

while True:
  i += 1
  L = i*lcm(N, M)
  N_list = [(L//N)*k + 1 for k in range(N)]
  M_list = [(L//M)*k + 1 for k in range(M)]

  memo = {}

  for idx, n in enumerate(N_list):
    memo[n] = S[idx]

  for idx, m in enumerate(M_list):
    if m in memo.keys():
      if memo[m] != T[idx]:
        break
  else:
    print(L)
    exit(0)

  if L == N*M:
    break

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)]
tmp1 = ['' for _ in range(lcm+1)]
tmp2 = ['' 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

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

    tmp2[ind] = T[i:i+1]

for i in range(lcm):
    s1,s2 = tmp1[i],tmp2[i]
    if s1 == '' or s2 == '' or s1 == s2:
        continue
    else:
        print(-1)
        exit()
print(lcm)

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


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


start = lcm(N, M)
ans = 0
for i in range(10000):
    L = (i+1) * start
    # L = 45
    pos_dic = {}
    ng_flg = 0

    if L < (N - 1) * (L / N) + 1 or L < (M - 1) * (L / M) + 1:
        continue

    for j in range(N):
        pos_dic[j * (L / N) + 1] = S[j]
    for k in range(M):
        if pos_dic.get(k * (L / M) + 1, None) is None:
            pass
        else:
            if pos_dic[k * (L / M) + 1] == T[k]:
                pass
            else:
                ng_flg = 1
                break

    if ng_flg == 0:
        ans = L
        break

if ans == 0:
    print('-1')
else:
    print(ans)

Submission status: Runtime Error

Solution code:
N, M = input().split()
S=input()
slen=len(S)
T=input()
tlen=len(T)
ans=slen*tlen
if S[0]=="a":
    print ("-1")
else:
  while True:
    tlen++