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: Accepted

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

k = N * M // gcd(N, M)
if  all(s == t for s, t in zip(S[::k // M], T[::k // N])):
    print(k)
else:
    print(-1)

Submission status: Accepted

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

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

g = gcd(N, M)
if S[0::N//g] == T[0::M//g]:
  print(N*M//g)
else:
  print(-1)

Submission status: Accepted

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

for k in range(min(n,m), 0, -1):
    if n%k==0 and m%k==0:
        l = k
        break

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

Submission status: Accepted

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



for i in range(1,M+1):
    if N*i%M==0:
        break

ANS=N*i

check=1
for i in range(N):
    s=S[i]
    if (i*M)%N==0:
        t=T[(i*M)//N]
        if s!=t:
            check=0
            #print(s,t)
            break

if check==1:
    print(ANS)
else:
    print(-1)

Submission status: Accepted

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

n,m=[int(i) for i in input().split()]
s=input()
t=input()

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

ok=1
for i in range(g):
#    print(i)
#    print(s[i*n//g],t[i*m//g])
    if s[i*n//g] != t[i*m//g]:
        ok=0


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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def get_gcd(a:int, b:int) -> int:
    while b:
        a, b = b, a % b
    return a
    
def get_lcm(a:int, b:int) -> int:
    return (a // get_gcd(a,b)) * b

n,m = li()
s = lc()
t = lc()

sx = dict()
tx = dict()

lcm = get_lcm(n,m)

for idx, i in enumerate(range(1,lcm+1,lcm//n)):
    sx.update({i:s[idx]})

for idx, j in enumerate(range(1,lcm+1,lcm//m)):
    tx.update({j:t[idx]})
    
common_key = list(set(list(sx.keys())) & set(list(tx.keys())))

exist = True
for c in common_key:
    if sx[c] != tx[c]:
        exist = False
        break
    
if exist:
    print(lcm)
else:
    print(-1)

Submission status: Accepted

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

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

G = gcd(N,M)
n = N//G
m = M//G

ans = -1
i = 0
while i*n < N:
    if S[i*n] != T[i*m]:
        break
    i+=1
else:
    ans = N*M//G

print(ans)

Submission status: Accepted

Solution code:
from fractions import gcd

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

gcd_ = gcd(N, M)
lcm_ = N * M // gcd_
for i in range(gcd_):
    if S[i * N // gcd_] != T[i * M // gcd_]:
        print(-1)
        quit()
print(lcm_)

Submission status: Accepted

Solution code:
import math
import sys
n,m = map(int,input().split())
s = input()
t = input()
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(a, b):
	return a * b // gcd (a, b)
ans = lcm(n,m)
n1 = ans//n
m1 = ans//m
atai = {}
for i in range(n):
  atai[i*n1+1] = s[i]
for i in range(m):
  kaku = atai.get(i*m1+1, -1)
  if kaku != -1 and kaku != t[i]:
    print(-1)
    exit()
print(ans)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
S = list(input())
T = list(input())
def gcd(a, b):
    while b > 0:
        a, b = b, a%b
    return a

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

L = lcm(N, M)
d1 = L//N
d2 = L//M
if S[::d2] == T[::d1]:
    print(L)
else:
    print(-1)

Submission status: Accepted

Solution code:
import sys
n1,n2 = list( map(int, input().split()) )
s1 = input()
s2 = input()

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

t = gcd(n1,n2)

re = n1*n2//t

x1 = re//n1
x2 = re//n2

step = x1*x2 // gcd(x1,x2)

ii = 0
while ii <re:
    j1 = ii // x1 
    j2 = ii // x2
    if s1[j1]!= s2[j2]:
        print(-1)
        sys.exit()
    ii+=step
print(re)

Submission status: Accepted

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

c = gcd(n, m)
flag = True
for i in range(c):
    if s[n * i // c] != t[m * i // c]:
        flag = False
        break
if flag == True:
    print(n * m // c)
else:
    print(-1)

Submission status: Accepted

Solution code:
def GCD(a, b):
    a, b = max(a, b), min(a, b)
    while a%b>0:
        a, b = b, a%b
    return b

N, M = map(int, input().split())
S = input()
T = input()
GCD = GCD(N, M)
LCM = N * M // GCD
sm, tm = N//GCD, M//GCD
for i in range(GCD):
    if S[i*sm] != T[i*tm]:
        print(-1)
        break
else:
    print(LCM)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

def main():
    n, m = map(int, input().split())
    s = input()
    t = input()
    k = gcd(n, m)
    if s[::n // k] == t[::m // k]:
        print(n * m // k)
    else:
        print(-1)

def gcd(x, y):
    if x < y:
        x, y = y, x  # x >= y
    while y > 0:
        r = x % y
        x = y
        y = r
    return x

main()

Submission status: Accepted

Solution code:
from fractions import gcd
N, M = map(int, input().split())
S = input()
T = input()
GCD = gcd(N, M)
s = S[::N//GCD]
t = T[::M//GCD]
if s != t:
  print(-1)
else:
  print(N * M // gcd(N, M))

Submission status: Accepted

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

g = gcd(N, M)
ans = N*M//g
n = ans//N
m = ans//M

for i in range(g):
    if S[i*m] != T[i*n]:
        print(-1)
        exit()
print(ans)

Submission status: Accepted

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)
if gcd(n,m)==1:
    if s[0]==t[0]:
        print(lcm(n,m))
    else:
        print(-1)
else:
    a,b=n//gcd(n,m),m//gcd(n,m)
    x=[]
    y=[]
    for i in range(0,n,a):
        x.append(s[i])
    for i in range(0,m,b):
        y.append(t[i])
    if x==y:
        print(lcm(n,m))
    else:
        print(-1)

Submission status: Accepted

Solution code:
from fractions import gcd
def main():
    n, m = map(int, raw_input().split())
    s = raw_input().strip()
    t = raw_input().strip()
    g = gcd(n, m)
    l = n * m / g
    a = l / n
    b = l / m
    d = {}
    for i, x in enumerate(s):
        d[i*a] = x
    for i, x in enumerate(t):
        if i * b in d and d[i*b] != x:
            print -1
            return
    print l
main()

Submission status: Accepted

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

"""
6 4
GCD=2
LCM=12
a.b.c.d.e.f.
a..b..c..d..
"""
def GCD(a, b):
    if a < b:
        a, b = b, a
    while b > 0:
        a, b = b, a%b
    return a

def LCM(a, b):
    return a * b // GCD(a, b)
  
X = LCM(N, M)
w = 0
while w < N*M:
  if S[w//M] == T[w//N]:
    w += X
    continue
  else:
    print(-1)
    exit(0)

print(X)

Submission status: Accepted

Solution code:
import fractions as frc
N,M=map(int,input().split())
S = input()
T = input()
L = (N*M)//frc.gcd(N,M)
n = L//N
m = L//M
d = {}
for i in range(N):
    d[n*i] = S[i]
for i in range(M):
    if m*i in d:
        if d[m*i] != T[i]:
            print(-1)
            exit()
print(L)

Submission status: Accepted

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

N,M = map(int,input().split())
S = input()
T = input()
k = gcd(N,M)
n = N//k
m = M//k
flg = False
for i in range(k):
    if S[n*i] != T[m*i]:
        flg = True
if flg:
    print(-1)
else:
    print(N*m)

Submission status: Accepted

Solution code:
from fractions import gcd

n,m = map(int,input().split())
s = input()
t = input()
if n==m:
    if s!=t:
        print('-1')
        exit()
    else:
        print(n)
        exit()
elif n<m:
    n,m = m,n
    s,t = t,s
# n>m
if n%m==0:
    k = n//m
    if s[::k]==t:
        print(n)
        exit()
    else:
        print('-1')
        exit()
else:
    g = gcd(n,m)
    if s[::n//g]==t[::m//g]:
        print(n*m//gcd(n,m))
    else:
        print(-1)

Submission status: Accepted

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

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

g = gcd(N,M)
l = N * M // g

n = N // g
m = M // g

for i in range(g):
    if S[n*i] != T[m*i]:
        print(-1)
        break
else:
    print(l)

Submission status: Accepted

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

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

A = gcd(N, M)

res = M * N // A

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

Submission status: Accepted

Solution code:
def gcd(x,y):
    a=max(x,y)
    b=min(x,y)
    if a%b==0:
        return b
    else:
        return gcd(a%b,b)
def lcm(x,y):
    return (x*y)//gcd(x,y)
N,M=map(int,input().split())
S=input()
T=input()
X=lcm(N,M)
Y=gcd(N,M)
A=N//Y
B=M//Y
for i in range(Y):
    if S[i*A]!=T[i*B]:
        print(-1)
        exit()
print(X)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,M=map(int,raw_input().split())

S= raw_input()
T= raw_input()

def gcd(a,b):
    g=1
    for i in range(1,min(a,b)+1):
        if a%i==0 and b%i==0:
            g=i
    return g
    
g=gcd(N,M)

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

if flag:
    print N*M/g
else:
    print -1

Submission status: Accepted

Solution code:
n,m = (int(i) for i in input().split())
s = input()
t = input()
def gcd(a,b):
	if a%b: return gcd(b,a%b)
	else: return b
g = gcd(n,m)
ng,mg = n//g,m//g
if all(s[i*ng]==t[i*mg] for i in range(g)): print(n*m//g)
else: print(-1)

Submission status: Accepted

Solution code:
import math
def gcd(a, b):
    #aとbの最大公約数を求める
    #math.gcdもしくはnp.gcdを使えばいいのだが、AtCoder上のpythonでは使えない
    if a < b:
        return gcd(b, a)
    if b == 0:
        return a
    else:
        c = a % b
        return gcd(b, c)

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)

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try: from typing import Any, Union, List, Tuple, Dict
except ImportError: pass
sys.setrecursionlimit(10**6)
def debug(*args): print(*args, file=sys.stderr)
def exit(): sys.exit(0)


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


def gcd(a, b):
    # type: (int, int) -> int
    "最大公約数を計算する．0とか負の数が入るとヤバい"
    if b%a == 0:
        return a
    return gcd(b%a, a)


d = gcd(N, M)
n = N // d
m = M // d


for i in range(d):
    if S[i*n] != T[i*m]:
        print(-1)
        exit()
print(n*m*d)

Submission status: Accepted

Solution code:
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict, Counter
from bisect import bisect
from fractions import gcd

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N, M = reads()
S = input()
T = input()
g = gcd(N, M)
a = N // g
b = M // g
possible = all(S[a*i] == T[b*i] for i in range(g))
print(N * b if possible else -1)

Submission status: Accepted

Solution code:
import fractions

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

g = fractions.gcd(N, M)
ans = N*M/g
for i in range(g):
  if S[i*N/g] != T[i*M/g]:
    ans = -1
print ans

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

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

prod=n
while prod%m!=0: prod+=n

d={}
for i in range(n):
    d[(prod//n)*i]=s[i]
for i in range(m):
    j=(prod//m)*i
    if (j in d) and (d[j]!=t[i]):
        print("-1")
        break
else:
    print(prod)

Submission status: Accepted

Solution code:
from fractions import gcd


def solve(n, m, s, t):
    g = gcd(n, m)
    ng = n // g
    mg = m // g

    for h in range(g):
        if s[ng * h] != t[mg * h]:
            return -1
    return ng * mg * g


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

Submission status: Runtime Error

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

# 良い文字列が作れるなら、答えはLCM(n, m)となる
lcm = n * m // math.gcd(n, m)

x = ['*'] * lcm
for i,c in enumerate(s):
    # (pos, char)
    x[lcm//n*i] = c

for i,c in enumerate(t):
    # (pos, char)
    pos, char = lcm//m*i, c
    if x[pos] != '*' and x[pos] != char:
        print(-1)
        exit()

print(lcm)

Submission status: Runtime Error

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

# 良い文字列が作れるなら、答えはLCM(n, m)となる
lcm = n * m // math.gcd(n, m)

S = len(s)
T = len(t)
x = ['*'] * lcm
for i,c in enumerate(s):
    # (pos, char)
    x[lcm//S*i] = c

for i,c in enumerate(t):
    # (pos, char)
    pos, char = lcm//T*i, c
    if x[pos] != '*' and x[pos] != char:
        print(-1)
        exit()

print(lcm)

Submission status: Runtime Error

Solution code:
import math

class common_function():
    """
        1. よく使いそうで予め用意しておいた変数をまとめた
        2. よく使いそうな関数群をまとめた
    """
    def lcm(self, x:int, y:int):
        """
            最小公倍数を返すメソッド
            math をインポートする
        """
        return (x * y) // math.gcd(x, y)
        

def main():
    common = common_function()
    N, M = map(int, input().split())
    S = input()
    T = input()
    lengthX = common.lcm(N, M)
    XdivS = lengthX//N
    XdivT = lengthX//M
    X = ['0'] * lengthX
    for i in range(N):
        j = XdivS*i
        X[j] = S[i]
    
    for i in range(M):
        j = XdivT*i
        if X[j] == '0':
            continue
        if X[j] != T[i]:
            print(-1)
            return
    print(lengthX)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

if n==1:
    if t == s*m:
        print(m)
    else:
        print(-1)
    exit(0)
elif m == 1:
    if s == t**n:
        print(n)
    else:
        print(-1)
    exit(0)

"""
最小公倍数の長さで収まらない場合は、何をやってもダメ
"""
dp = [0] * ans
for i in range(n):
    dp[i*ans//n] = s[i]

for i in range(m):
    if dp[i*ans//m] == 0: 
        dp[i*ans//m] = t[i]
    else:
        if dp[i*ans//m] != t[i]:
            print(-1)
            exit(0)
print(ans)

Submission status: Runtime Error

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

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

"""
最小公倍数の長さで収まらない場合は、何をやってもダメ
"""
dp = [0] * ans
for i in range(n):
    dp[i*ans//n] = s[i]

for i in range(m):
    if dp[i*ans//m] == 0: 
        dp[i*ans//m] = t[i]
    else:
        if dp[i*ans//m] != t[i]:
            print(-1)
            exit(0)
print(ans)

Submission status: Runtime Error

Solution code:
import sys, math
from functools import lru_cache
from collections import defaultdict
from decimal import Decimal
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

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

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N, M = mi()
    S, T = [list(input()), list(input())]
    l = M*N//math.gcd(M, N)

    s = ['']*l
    t = ['']*l

    s[::l//N] = S
    t[::l//M] = T

    for i in range(l):
        if not (s[i] == '' or t[i] == '' or s[i] == t[i]):
            print(-1)
            return

    print(l)




if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*L

i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i += 1
    print(lis)
    
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            ans = -1
            break
    print(lis)
else:
    ans = L
print(ans)

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    if i == N:
        break
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if j == M:
        break
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            L = -1
            break
ans = L
print(ans)

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    if i == len(S):
        break
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if j == len(T):
        break
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
        else:
            L = -1
            break
ans = L
print(ans)

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*(L)
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*(L+1)
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)

Submission status: Runtime Error

Solution code:
#66 A - Two Abbreviations
import math
N,M = map(int,input().split())
S = input()
T = input()

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

dist_N = L//N
dist_M = L//M
lis = [0]*L
i = 0
for n in range(0,L,dist_N):
    lis[n] = S[i]
    i+=1
j = 0
for m in range(0,L,dist_M):
    if lis[m] == 0:
        lis[m] = T[j]
        j += 1
    else:
        if lis[m] == T[j]:
            j += 1
            continue
        else:
            ans = -1
            break
else:
    ans = L
print(ans)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = n
while a % m != 0:
    a += n
    if a >= 10 ** 10:
        print(-1)
        exit(0)
l = ['0'] * a
s = input()
t = input()
for i in range(n):
    l[a//n*i] = s[i]
for i in range(m):
    if l[a//m*i] != '0':
        if l[a//m*i] != t[i]:
            print(-1)
            exit(0)
    else:
        l[a//m*i] = t[i]
print(a)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = n
while a % m != 0:
    a += n
    if a >= 10 ** 12:
        print(-1)
        exit(0)
l = ['0'] * a
s = input()
t = input()
for i in range(n):
    l[a//n*i] = s[i]
for i in range(m):
    if l[a//m*i] != '0':
        if l[a//m*i] != t[i]:
            print(-1)
            exit(0)
    else:
        l[a//m*i] = t[i]
print(a)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
a = n
while a % m != 0:
    a += n
l = ['0'] * a
s = input()
t = input()
for i in range(n):
    l[a//n*i] = s[i]
for i in range(m):
    if l[a//m*i] != '0':
        if l[a//m*i] != t[i]:
            print(-1)
            exit(0)
    else:
        l[a//m*i] = t[i]
print(a)

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    size = lcm(m, n)
    X = [None] * (size + 1000)
    S = input()
    T = input()
    try:
        for i in range(n):
            X[i * (size // n) + 1] = S[i]
        for i in range(m):
            if X[i * (size // m) + 1] == T[i]:
                continue
            elif not X[i * (size // m) + 1]:
                X[i * (size // m) + 1] = T[i]
            else:
                print(-1)
                return
        print(size)
    except BaseException:
        print(-1)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/agc028/tasks/agc028_a

import sys
# sys.setrecursionlimit(100000)


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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]
# Greatest Common Divisor(Euclidean algorithm)


def gcd(x: int, y: int) -> int:
    if x < y:
        x, y = y, x
    while y > 0:
        r = x % y
        x, y = y, r
    return x


# Least Common Multiple
def lcm(x: int, y: int) -> int:
    _gcd = gcd(x, y)
    return (x * y) // _gcd


def main():
    n, m = input_int_list()
    size = lcm(m, n)
    X = [None] * (size + 1)
    S = input()
    T = input()
    for i in range(n):
        X[i * (size // n) + 1] = S[i]
    for i in range(m):
        if X[i * (size // m) + 1] == T[i]:
            continue
        elif not X[i * (size // m) + 1]:
            X[i * (size // m) + 1] = T[i]
        else:
            print(-1)
            return
    print(size)
    return


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from math import gcd
def main():
    n, m = map(int, input().split())
    s = tuple(input())
    t = tuple(input())

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
            sys.exit()
        else:
            print(-1)
            sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
        sys.exit()

    l = (n * m) // gcd(n, m)
    x = [0] * l
    ld1 = l // l1
    ld2 = l // l2
    for j1 in range(1, l1):
        x[j1 * ld1] = s1[j1]
    for k1 in range(1, l2):
        if x[k1 * ld2]:
            if x[k1 * ld2] != s2[k1]:
                print(-1)
                sys.exit()
    print(l)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
            sys.exit()
        else:
            print(-1)
            sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
        sys.exit()

    l = (n * m) // gcd(n, m)
    x = [0] * l
    ld1 = l // l1
    ld2 = l // l2
    for j1 in range(1, l1):
        x[j1 * ld1] = s1[j1]
    for k1 in range(1, l2):
        if x[k1 * ld2]:
            if x[k1 * ld2] != s2[k1]:
                print(-1)
                sys.exit()
    print(l)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

    if s[0] != t[0]:
        print(-1)
        sys.exit()
    if n == m:
        if s == t:
            print(n)
        else:
            print(-1)
        sys.exit()
    if n > m:
        l1 = n
        l2 = m
        s1 = s
        s2 = t
    else:
        l1 = m
        l2 = n
        s1 = t
        s2 = s
    if n % m == 0 or m % n == 0:
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)
    else:
        l = (n * m) // gcd(n, m)
        x = [0] * l
        ld1 = l // l1
        ld2 = l // l2
        for j1 in range(1, l1):
            x[j1 * ld1] = s1[j1]
        for k1 in range(1, l2):
            if x[k1 * ld2]:
                if x[k1 * ld2] != s2[k1]:
                    print(-1)
                    sys.exit()
        print(l)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


N, M = map(int, input().split())
s = input()
t = input()
L = lcm(N, M)
X = [''] * L
for i in range(N):
    X[i * (L // N)] = s[i]
for i in range(M):
    if X[i * (L // M)] == '':
        X[i * (L // M)] = t[i]
    elif X[i * (L // M)] == t[i]:
        continue
    else:
        print(-1)
        exit()
# print(X)
print(L)

Submission status: Runtime Error

Solution code:
# 02
import sys
import math


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n, m = LI()
s = S()
t = S()

gcd = math.gcd(n, m)
l = n * m // gcd
d_s = l // n
d_t = l // m
x = list('*' * l)
is_ok = True

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

for i in range(m):
    if x[d_t * i] != '*':
        if x[d_t * i] != t[i]:
            is_ok = False
            break
    else:
        x[d_t * i] = t[i]

if is_ok:
    ans = len(x)
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
# 02
import sys
import math


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n, m = LI()
s = S()
t = S()

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

x = list('*' * l)
is_ok = True
for i in range(n):
    x[d_s * i] = s[i]
for i in range(m):
    if x[d_t * i] != '*' and x[d_t * i] != t[i]:
        is_ok = False
        break
    x[d_t * i] = t[i]
if is_ok:
    ans = len(x)
    print(ans)
else:
    print(-1)

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
s = list(input())
t = list(input())
ans = n
while ans % m != 0:
    ans += n
l = ['0'] * ans
j = 0
for i in range(n):
    z = j * (ans // n)
    l[z] = s[i]
    j += 1
j = 0
for i in range(m):
    z = j * (ans // m)
    if l[z] == '0' or l[z] == t[i]:
        l[z] = s[i]
        j += 1
    else:
        print(-1)
        exit(0)
print(ans)

Submission status: Runtime Error

Solution code:
from math import gcd
def lcm(x,y):return x*y//gcd(x,y)
A,B = map(int,input().split())
sa = input()
sb = input()
L = lcm(A,B)
s = [''] * (L+1)
res = L
for i in range(len(sa)):
    ind = i*(L//A)
    s[ind] = sa[i]
for i in range(len(sb)):
    ind = i*(L//B)
    if s[ind] and s[ind] != sb[i]:
        res = -1
print(res)

Submission status: Runtime Error

Solution code:
from math import gcd
def lcm(x,y):return x*y//gcd(x,y)
A,B = map(int,input().split())
sa = input()
sb = input()
L = lcm(A,B)
s = [''] * L
res = L
for i in range(len(sa)):
    ind = i*(L//A)
    s[ind] = sa[i]
for i in range(len(sb)):
    ind = i*(L//B)
    if s[ind] and s[ind] != sb[i]:
        res = -1
print(res)

Submission status: Runtime Error

Solution code:
from math import gcd
def lcm(x,y):return x*y//gcd(x,y)
A,B = map(int,input().split())
sa = input()
sb = input()
L = lcm(A,B)
s = [0] * L
res = L
for i in range(len(sa)):
    ind = i*(L//A)
    s[ind] = sa[i]
for i in range(len(sb)):
    ind = i*(L//B)
    if s[ind] != 0 and s[ind] != sb[i]:
        res = -1
print(res)