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 fractions
import sys
sys.setrecursionlimit(10**6)

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm
flag = True

c = 0
for i in range(0, (N-1) * step1 + 1, step1):
    if c >= N:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, (M-1) * step2 + 1, step2):
    if c >= M:
        break
    if X[i] != 0 and X[i] != T[c]:
        flag = False
        break
    X[i] = T[c]
    c += 1

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

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm
flag = True

c = 0
for i in range(0, (N-1) * step1, step1):
    if c >= N:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, (M-1) * step2, step2):
    if c >= M:
        break
    if X[i] != 0 and X[i] != T[c]:
        flag = False
        break
    X[i] = T[c]
    c += 1
    
if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm
flag = True

c = 0
for i in range(0, (N-1) * step1 + 1, step1):
    if c >= N:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, (M-1) * step2 + 1, step2):
    if c >= M:
        break
    if X[i] != 0 and X[i] != T[c]:
        flag = False
        break
    X[i] = T[c]
    c += 1
    
if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm
flag = True

c = 0
for i in range(0, lcm, step1):
    if c >= N:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, lcm, step2):
    if c >= M:
        break
    if X[i] != 0 and X[i] != T[c]:
        print(-1)
        flag = False
        break
    X[i] = T[c]
    c += 1
    
if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm

c = 0
for i in range(0, lcm, step1):
    if c >= N:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, lcm, step2):
    if c >= M:
        break
    if X[i] != 0 and X[i] != T[c]:
        print(-1)
        flag = False
        break
    X[i] = T[c]
    c += 1
    
if flag:
    print(lcm)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm

c = 0
for i in range(0, lcm, step1):
    if c > len(S) - 1:
        break
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, lcm, step2):
    if c > len(T) - 1:
        break
    if X[i] != 0 and X[i] != T[c]:
        print(-1)
        exit()
    X[i] = T[c]
    c += 1

print(lcm)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * (10 ** 6)

c = 0
for i in range(0, lcm, step1):
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, lcm, step2):
    if X[i] != 0 and X[i] != T[c]:
        print(-1)
        exit()
    X[i] = T[c]
    c += 1

print(lcm)

Submission status: Runtime Error

Solution code:
import fractions

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

lcm = N * M // fractions.gcd(N, M)

step1 = lcm // N
step2 = lcm // M

X = [0] * lcm

c = 0
for i in range(0, lcm, step1):
    X[i] = S[c]
    c += 1

c = 0
for i in range(0, lcm, step2):
    if X[i] != 0 and X[i] != T[c]:
        print(-1)
        exit()
    X[i] = T[c]
    c += 1

print(lcm)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
from fractions import gcd
L = N*M//gcd(N,M)
res = [0]*(L)
cnt1 = 0
for s in S:
    res[cnt1*L//N] = s
    cnt1 += 1
cnt2 = 0
for t in T:
    if res[cnt2*L//M] == 0 or res[cnt2*L//M] == t:
        res[cnt2*L//M] = t
        cnt2 += 1
    else:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
from fractions import gcd
L = N*M//gcd(N,M)
res = ['!']*(10**10)
cnt1 = 0
for s in S:
    if cnt1*L//N > L:
        print(-1)
        exit()
    res[cnt1*L//N] = s
    cnt1 += 1
cnt2 = 0
for t in T:
    if cnt2*L//M > L:
        print(-1)
        exit()
    if res[cnt2*L//M] == '!' or res[cnt2*L//M] == t:
        res[cnt2*L//M] = t
        cnt2 += 1
    else:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
from fractions import gcd
L = N*M//gcd(N,M)
res = ['!']*(L+100)
cnt1 = 0
for s in S:
    if cnt1*L//N > L:
        print(-1)
        exit()
    res[cnt1*L//N] = s
    cnt1 += 1
cnt2 = 0
for t in T:
    if cnt2*L//M > L:
        print(-1)
        exit()
    if res[cnt2*L//M] == '!' or res[cnt2*L//M] == t:
        res[cnt2*L//M] = t
        cnt2 += 1
    else:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
from fractions import gcd
L = N*M//gcd(N,M)
res = ['!']*(L+100)
cnt1 = 0
for s in S:
    res[cnt1*L//N] = s
    cnt1 += 1
cnt2 = 0
for t in T:
    if res[cnt2*L//M] == '!' or res[cnt2*L//M] == t:
        res[cnt2*L//M] = t
        cnt2 += 1
    else:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
from fractions import gcd
L = N*M//gcd(N,M)
res = ['!']*(L+1)
cnt1 = 0
for s in S:
    res[cnt1*L//N] = s
    cnt1 += 1
cnt2 = 0
for t in T:
    if res[cnt2*L//M] == '!' or res[cnt2*L//M] == t:
        res[cnt2*L//M] = t
        cnt2 += 1
    else:
        print(-1)
        exit()
print(L)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
S=input()
T=input()
g=gcd(N,M)
m=(N//g)*(M//g)*g
a=0
b=0
A=m//N
B=m//M
t=True
while(a<N):
    if S[a]==T[b]:
        a+=B
        b+=A
    else:
        print(-1)
        quit()
print(m)

Submission status: Runtime Error

Solution code:
from sys import exit
import fractions
n,m=map(int,input().split())
s=input()
t=input()


print((m*n)//gcd)

Submission status: Runtime Error

Solution code:
from fractions import gcd

lcm = lambda x, y: x*y//gcd(x, y)

N, M = map(int, input().split())
s = input(
t = input()
l = lcm(N, M)
n, m = N//l, M//l

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

Submission status: Runtime Error

Solution code:
def gcd(a,b):
	x,y = min(a,b), max(a,b)
	while x*y>0:
		x,y = y-x,x
	return y

N,M=map(int,input().split())
S=input()
T=input()
g=gcd(N,M)
p,q=M//g,N//g
print((N//g)*M) if S[0::q]==T[0::p] else print('-1')

Submission status: Runtime Error

Solution code:
def gcd(a,b):
	x,y = min(a,b), max(a,b)
	while x*y>0:
		x,y = y-x,x
	return y

N,M=map(int,input().split())
S=input()
T=input()
g=gcd(N,M)
p,q=M//g,N//g
print(N*M//g) if S[0::q]==T[0::p] else print('-1')

Submission status: Runtime Error

Solution code:
def lcm(a,b):
	x,y = min(a,b), max(a,b)
	while x*y>0:
		x,y = y-x,x
	return int(a*b/y)

N,M=map(int,input().split())
S=input()
T=input()
l=lcm(N,M)
p,q=l//N,l//M
print(l) if S[0::q]==T[0::p] else print('-1')

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math
import numpy as np

N = int(input())
A = list(map(int, input().rstrip().split(' ')))

sum_P = []
for i in range(N):
    temp = 0
    for j in range(N):
        #temp += 1 / (1 + abs(i - j))
        temp = 0.1
    sum_P.append(temp)

#cost = np.sum(np.array(sum_P) * A)
    
#print(int(round(cost * math.factorial(N))) % (1_000_000_007))

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math
import numpy as np

N = int(input())
A = list(map(int, input().rstrip().split(' ')))

sum_P = []
for i in range(N):
    temp = 0
    for j in range(N):
        temp += 1 / (1 + abs(i - j))
    sum_P.append(temp)

#cost = np.sum(np.array(sum_P) * A)
    
#print(int(round(cost * math.factorial(N))) % (1_000_000_007))

Submission status: Runtime Error

Solution code:
from fractions import gcd

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

ans = (a * b) // gcd(a, b)

if ans == N or ans == M:
    print(-1)
else:
    print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

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

L = lcm(N, M)

dp = [-1] * (L + 10)
'''   
for i, s in enumerate(S):
    dp[i*L//N] = s

for i, t in enumerate(T):
    if dp[i*L//M] != -1:
        if dp[i*L//M] != t:
            print(-1)
            sys.exit()
print(L)
'''

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

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

L = lcm(N, M)

dp = [-1] * (L + 10)
for i, s in enumerate(S):
    dp[i*L//N] = s
'''   
for i, t in enumerate(T):
    if dp[i*L//M] != -1:
        if dp[i*L//M] != t:
            print(-1)
            sys.exit()
print(L)
'''

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

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

L = lcm(N, M)

dp = [-1] * (L + 10)
for i, s in enumerate(S):
    dp[i*L//N] = s

flag = False   
for i, t in enumerate(T):
    if dp[i*L//M] != -1:
        if dp[i*L//M] != t:
            print(-1)
            flag = True
            break
            #sys.exit()
if flag == False:
    print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

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

L = lcm(N, M)

dp = [-1] * (L + 10)
for i, s in enumerate(S):
    dp[i*L//N] = s
   
for i, t in enumerate(T):
    if dp[i*L//M] != -1:
        if dp[i*L//M] != t:
            print(-1)
            sys.exit()
print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

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

L = lcm(N, M)

dp = [-1] * (L + 10)
for i, s in enumerate(S):
    dp[i*L//N] = s
    
for i, t in enumerate(T):
    if dp[i*L//M] != -1 and dp[i*L//M] != t:
        print(-1)
        sys.exit()
print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import fractions
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)
    
L = lcm(N, M)

dp = [-1] * L
for i, s in enumerate(S):
    dp[i*L//N] = s
    
for i, t in enumerate(T):
    if dp[i*L//M] != -1 and dp[i*L//M] != t:
        print(-1)
        sys.exit()
print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

def lcm(x, y):
    return int((x * y) / math.gcd(x, y))
    
L = lcm(N, M)

if S[0] != T[0]:
    print(-1)
    #sys.exit()
elif L == N or L == M:
    dp = [-1] * L
    for i, s in enumerate(S):
        dp[i*L/N] = s
        
    for i, t in enumerate(T):
        if dp[i*L/M] != -1 and dp[i*L/M] != t:
            print(-1)
            #sys.exit()
else:
    print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

def lcm(x, y):
    return int((x * y) / math.gcd(x, y))
    
L = lcm(N, M)
if S[0] != T[0]:
    print(-1)
    sys.exit()
elif L == N or L == M:
    dp = [-1] * L
    for i, s in enumerate(S):
        dp[i*L/N] = s
        
    for i, t in enumerate(T):
        if dp[i*L/M] != -1 and dp[i*L/M] != t:
            print(-1)
            sys.exit()
else:
    print(L)

Submission status: Runtime Error

Solution code:
# coding: utf-8
# Your code here!

import math
import sys

N, M = map(int, input().rstrip().split(' '))
S = [x for x in input()]
T = [x for x in input()]

def lcm(x, y):
    return (x * y) // math.gcd(x, y)
    
L = lcm(N, M)
if S[0] != T[0]:
    print(-1)
    sys.exit()
elif L == N or L == M:
    dp = [-1] * L
    for i, s in enumerate(S):
        dp[i*L//N] = s
        
    for i, t in enumerate(T):
        if dp[i*L//M] != -1 and dp[i*L//M] != t:
            print(-1)
            sys.exit()
else:
    print(L)

Submission status: Runtime Error

Solution code:
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
inf = 10**10
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(a,b):
  return a*b //gcd(a,b)
n,m = map(int,input().split())
s = input()
t = input()
d = gcd(n,m)
l = lcm(n,m)
a = n//d 
b = m//d 
for i in range(d):
    if s[a*i]!= t[b*i]:
        print(-1)
        break
    else:
        print(l)

Submission status: Runtime Error

Solution code:
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
from collections import Counter
from itertools import*
from bisect import bisect_left,bisect_right
inf = 10**10
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def gcd_list(numbers):
    return reduce(gcd,numbers)
def lcm(a,b):
  return a*b //gcd(a,b)
n,m = map(int,input().split())
s = input()
t = input()
d = gcd(n,m)
l = lcm(n,m)
a = n//d 
b = m//d 
for i in range(d):
    if s[a*i]!= t[b*i]:
        print(-1)
        break
    else:
        print(l)

Submission status: Runtime Error

Solution code:
import math
n, m = map(int, input().split())
s = input()
t = input()
lcm = (n * m) // math.gcd(n, m)
gcd = math.gcd(n, m)
ans = lcm
if gcd > 1:
    for i in range(gcd):
        if s[i * (n // gcd)] != t[i * (m // gcd)]:
            ans = -1
print(ans)

Submission status: Runtime Error

Solution code:
import math
n, m = map(int, input().split())
ns = input()
ms = input()

# n, m = 10**5, 31111
# print(math.gcd(n, m))

gcd = math.gcd(n, m)
lcm = n * m // gcd
# print(lcm, lcm // n, lcm // m)
lcmN = lcm // n
lcmM = lcm // m

# for i in range(lcm):
#     if (i % (lcmN)) == 0:
#         print(ns[i // lcmN], end="")
#     else:
#         print(".", end="")
# print()
# for i in range(lcm):
#     if (i % (lcmM)) == 0:
#         print(ms[i // lcmM], end="")
#     else:
#         print(".", end="")

s = []
for i in range(0, n, lcmM):
    s.append(ns[i])
for i in range(0, m, lcmN):
    if ms[i] != s[i // lcmN]:
        print(-1)
        exit()
print(lcm)

Submission status: Runtime Error

Solution code:
from fractions import gcd

lcm = lambda x, y: x*y//gcd(x, y)

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

L = lcm(n, m)
ans = ["0"]*L

for i in range(n):
    p = i*L//n
    ans[p] = s[i]

for j in range(m):
    q = j*L//m
    if ans[q] != "0" and ans[q] != t[j]:
        print(-1)
        exit()

print(L)

Submission status: Runtime Error

Solution code:
N,M = map(int,input().split())
S = input()
T = input()
flag = True
def lcm(a, b): # 最小公倍数
    def gcd(a, b): # 最大公約数
        while b:
            a, b = b, a % b
        return a
    return a * b // gcd(a, b)
L = lcm(N,M)
"""
l = [-1 for i in range(L)]
for i in range(N):
    l[L//N*i] = ord(S[i])-97
for i in range(M):
    if l[L//M*i] == -1:
        l[L//M*i] = ord(T[i])-97
    elif l[L//M*i] == ord(T[i])-97:
        pass
    else:
        flag = False
"""
if S[0] == T[0 and S[N-1] == T[M-1]:
    flag = True
else:
    flag = False
if flag:
    print(L)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

N, M = LI()
S = input()
T = input()
#  N, M = 10**5, 10**5
#  S = 's'*N
#  T = 't'*M

math.gcd(18,4)
print(32131)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

N, M = LI()
S = input()
T = input()
#  N, M = 10**5, 10**5
#  S = 's'*N
#  T = 't'*M

X = lcm(N, M)
print(32131)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

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

N, M = LI()
S = input()
T = input()
#  N, M = 10**5, 10**5
#  S = 's'*N
#  T = 't'*M

X = lcm(N, M)
L = X
XS = ['X' for i in range(L+1)]
XT = ['X' for i in range(L+1)]
print(122232)

Submission status: Runtime Error

Solution code:
from fractions import *
def lcm(x, y):
    return (x * y) // gcd(x, y)
N,M = map(int,input().split())
if N[0] != M[0]:
    print(-1)
    exit()
L = lcm(N,M)
words = [[] for i in range(L)]
S = str(input())
words[0].append(S[0])
for i in range(1,N):
    num = (L // N) * i
    words[num].append(S[i])
T = str(input())
for i in range(1,M):
    num = (L // M) * i
    if len(words[num]) == 1:
        if [T[i]] != words[num]:
            print(-1)
            exit()
    else:
        words[num].append(T[i])
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)
n = N // g
m = M // g
out = n * m * g
for k in range(g):
  if S[k * n] != T[k * m]:
    out = -1
    break
print(out)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = input()
T = input()
g = math.gcd(N, M)
flag = True
n = N // g
m = M // g
for k in range(g):
  flag *= int(S[n*k] == T[m*k])
if flag == 0:
  out = -1
else:
  out = n * m * g
print(out)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = input()
T = input()
g = math.gcd(N, M)
flag = True
n = int(N / g)
m = int(M / g)
for k in range(g):
  flag *= int(S[n*k] == T[m*k])
if flag == 0:
  out = -1
else:
  out = n * m * g
print(out)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = input()
T = input()
g = math.gcd(N, M)
flag = True
n = int(N / g)
m = int(M / g)
for k in range(g):
  flag *= int(S[n*m*k] == T[n*m*k])
if flag == 0:
  out = -1
else:
  out = n * m * g
print(out)

Submission status: Runtime Error

Solution code:
import math
N, M = map(int, input().split())
S = input()
T = input()
g = math.gcd(N, M)
flag = True
n = N / g
m = M / g
for k in range(g):
  flag *= (S[n*m*k] == T[n*m*k])
if flag == 0:
  out = -1
else:
  out = n * m * g
print(out)

Submission status: Runtime Error

Solution code:
N,M=map(int,input().split())
S=input()
T=input()
ans=["-1"]*()
def computeGCD(x, y): 
  if x > y: 
    small = y    
  else: 
    small = x
  for i in range(1, small+1): 
    if((x % i == 0) and (y % i == 0)): 
      gcd = i 
  return gcd 

lena=computeGCD(N,M)
glena=N*M//lena#45 , 15,9 , 3,5 
comm=computeGCD(glena//N, glena//M)
gcomm=(glena//N)*(glena//M)//comm
for i in range(glena//gcomm):
  if S[i*glena//M]!=T[i*glena//N]:
    print(-1)
    exit()
print(glena)

Submission status: Runtime Error

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(N, M)
lcm = N * M // gcd_
s_ = lcm//N
t_ = lcm // M

X = ["0"] * (lcm*2)
for i in range(N):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(M):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
        break
    else:
        X[i * t_] = T[i]
if ans:
    print(lcm)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(lcm)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * (saisho*2)
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * (saisho+1)
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
gcd_ = gcd(len(S), len(T))
saisho = len(S) * len(T) // gcd_
s_ = saisho//len(S)
t_ = saisho//len(T)

X = ["0"] * saisho
for i in range(len(S)):
    #print(i, i * s_)
    X[i * s_] = S[i]
ans = True
for i in range(len(T)):
    if X[i * t_] != "0" and T[i] != X[i * t_]:
        ans = False
    else:
        X[i * t_] = T[i]
if ans:
    print(saisho)
else:
    print("-1")
"""
ans = True
if gcd_ != 1:
    for i in range(gcd_):
        # print(i)
        if S[s_ * i] != T[t_ * i]:
            ans = False
            # break
if ans:
    print(saisho)
else:
    print(-1)
"""

Submission status: Runtime Error

Solution code:
def main28():
    import sys
    N, M = (int(x) for x in input().split())

    if N >= M:
        S = input()
        T = input()
    else:
        T = input()
        S = input()

    def lcm(n, m):
        gcd = 0
        for i in range(1, min(n, m)):
            if n % i == 0 and m % i == 0:
                gcd = i

        return int(n * m / gcd)

    LCM = lcm(N, M)
    a = int(LCM / max(N, M))
    b = int(LCM / min(N, M))

    for i in range(min(N, M)):
        small_index = i * b
        if int(small_index % a) == 0:
            if S[int(small_index / a)] != T[i]:
                print("-1")
                sys.exit()
    print(LCM)

main28()

Submission status: Runtime Error

Solution code:
def main28():
    import sys
    N, M = (int(x) for x in input().split())

    if N >= M:
        S = input()
        T = input()
    else:
        T = input()
        S = input()

    def lcm(n, m):
        gcd = 0
        for i in range(1, min(n, m)):
            if n % i == 0 and m % i == 0:
                gcd = i

        return int(n * m / gcd)

    LCM = lcm(N, M)
    a = int(LCM / max(N, M))
    b = int(LCM / min(N, M))

    for i in range(b):
        small_index = i * b
        if small_index % a == 0 and S[int(small_index / a)] != T[i]:
            print("-1")
            sys.exit()
    print(LCM)

main28()

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

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

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

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

Submission status: Runtime Error

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

def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if y == 0:
        return x
    else:
        return gcd(y, x%y)

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


L = lcm(N, M)
X = {}


for i in range(0,N):
	X[i*L//N] = S[i]
	# X[i*L//N] = S[i]
for i in range(0,M):
	if T[i] in X.values():
		if X[i*L//M] != T[i]:
			print(-1)
			exit()
	else:
		pass
print(L)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
S = input()
T = input()
 
def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if y == 0:
        return x
    else:
        return gcd(y, x%y)
 
def lcm(m, n):
	return int(m*n/gcd(m,n))
L = lcm(N, M)
X = ['0'] * 10**5 * (10**5 - 1)
 
print(lcm(N, M))

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
S = input()
T = input()
 
def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if y == 0:
        return x
    else:
        return gcd(y, x%y)
 
def lcm(m, n):
	return int(m*n/gcd(m,n))
L = lcm(N, M)
X = ['0'] * L

print(lcm(N, M))

Submission status: Runtime Error

Solution code:
n, x = map(int, input().split())
a = sorted(list(map(int, input().split())))
for i in range(n) :
  x -= a[i]
  if(x == 0) : 
    print(i+1)
    exit()
  elif(a[i] > x) : print(i)

Submission status: Runtime Error

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

def gcd(m, n):
    x = max(m, n)
    y = min(m, n)
    if y == 0:
        return x
    else:
        return gcd(y, x%y)

def lcm(m, n):
	return int(m*n/gcd(m,n))
L = lcm(N, M)
X = ['0'] * L

for i in range(0,N):
	X[i*L//N] = S[i]
for i in range(0,M):
	if X[i*L//M] != '0':
		if X[i*L//M] != T[i]:
			print(-1)
			exit()
	else:
		pass
print(lcm(N, M))

Submission status: Runtime Error

Solution code:
import numpy as np
N,M=map(int,input().split())
S = input()
T = input()
g = np.gcd(M,N)
n = N//g
m = M//g
l = N*M // g
f = True
for i in range(g):
    a = n*i
    b = m*i
    if S[a] != T[b]:
        f = False
print(l if f else -1)

Submission status: Runtime Error

Solution code:
import math
N,M=map(int,input().split())
S = input()
T = input()
g = math.gcd(N,M)
n = N//g
m = M//g
l = N*M // g
f = True
for i in range(g):
    a = n*i
    b = m*i
    if S[a] != T[b]:
        f = False
print(l if f else -1)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = input()
t = input()
#greatest common divisor/least common multiple
import fractions
gcd = int(fractions.gcd(n,m))
lcm = int(n*m/gcd)
counter=0
for i in range(gcd):
    if s[int(n/gcd)*i+1]!=t[int(m/gcd)*i+1]:
        print(-1)
        counter+=1
        exit()
if counter == 0:
    print(lcm)