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: Wrong Answer

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

a = ""
b = ""
c = 1

import fractions

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

l = lcm(m, n)

if l == m or l == n:

    for i in range(n):
        a = a + s[i] + "A" * int((l / n)-1)

    for i in range(m):
        b = b + t[i] + "A" * int((l / m)-1)

    for i in range(len(a)):
        if (a[i] == b[i]) or a[i] == "A" or b[i] == "A":
            c = c
        else:
            c = 0

if c == 0:
    print(-1)
else:
    print(l)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys


input = sys.stdin.readline
INF = float('inf')


def inpl(): return list(map(int, input().split()))


# 整数(x, y)最大公約数を求める
def gcd(x, y):
    if x % y == 0:
        return y
    else:
        x, y = y, x % y
        return gcd(x, y)


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


N, M = inpl()
S = input()
T = input()

L = lcm(N, M)
LCM = lcm(L//N, L//M) + 1
# print(L, LCM)

ss = L // N
cnt = 0
tmp = 0
S_li = [S[0]]
while tmp <= L:
    tmp = ss * cnt + 1
    if tmp % LCM == 0:
        S_li.append(S[cnt])
    cnt += 1

tt = L // M
cnt = 0
tmp = 0
T_li = [T[0]]
while tmp < L:
    tmp = tt * cnt + 1
    if tmp % LCM == 0:
        T_li.append(T[cnt])
    cnt += 1

# print(S_li)
# print(T_li)
if S_li == T_li:
    print(L)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N,M=map(int,input().split())
A=max(N,M)
B=min(N,M)
if A%B==0:
    gcd=B
    lcm=A
else:
    gcd=1
    while A%B!=0:
        A1=A%B
        A2=B
        A=max(A1,A2)
        B=min(A1,A2)
        gcd=B
    
    lcm=int((N*M)/gcd)
#print(lcm)

S=input()
T=input()

if len(S)==len(T):
    if S==T:
        print(len(S))
    else:
        print(-1)
elif lcm==max(len(S),len(T)):
    if len(S)>len(T):
        if S==T*int(lcm/len(T)):
            print(lcm)
        else:
            print(-1)
    else:
        if T==S*int(lcm/len(S)):
            print(lcm)
        else:
            print(-1)
else:
    if S[0]==T[0]:
        print(lcm)
    else:
        print(-1)

Submission status: Wrong Answer

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 s[0]!=t[0]:
    print('-1')
    exit(0)

renl=lcm(n,m)
if renl!=n and renl!=m:
    print(renl)
    exit(0)
if n>=m:
    mx=s
    mn=t
else:
    mx=t
    mn=s
lmn=len(mn)
for i in range(lmn):
    if mx[i*renl//lmn]!=mn[i]:
        print('-1')
        exit(0)
print(renl)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

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

try:

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

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

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

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

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

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

except:
    print(-1)

Submission status: Wrong Answer

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

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

N,M = map(int,input().split())
S = list(input())
T = list(input())
if N % M == 0 or S[0] != T[0] :
    print(-1)
else :
    print(lcm(N,M))

Submission status: Wrong Answer

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

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

if s[0] != t[0]:
    print(-1)
    exit()

g = lcm(n,m)

if g == n or g == m:
    if n < m:
        sm = n
        ls = s
        bs = t
    else:
        sm = m
        ls = t
        bs = s
    a = g//sm
    flg = False
    for i in range(sm):
        if bs[i * a] != ls[i]:
            flg = True
            break
    if flg:
        print(-1)
    else:
        print(g)
else:
    print(g)

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

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

x=gcd(n,m)

if x==1:
    print(n*m)
elif n==m:
    for i in range(n):
        if t[i]!=s[i]:
            print(-1)
            sys.exit()
    print(n)
elif x==n:
    for i in range(0,m,n):
        if t[i]!=s[i//n]:
            print(-1)
            sys.exit()
    print(n*m//x)
elif x==m:
    for i in range(0,n,m):
        if s[i]!=t[i//m]:
            print(-1)
            sys.exit()
    print(n*m//x)
else:
    if s[-1]!=t[-1] or s[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)

Submission status: Wrong Answer

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

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

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

ans = lcm(N, M)

i = 0
res = [0 for i in range(ans)]

while int(ans/N) * i < N:
    tmp_s = int(ans/N) * i
    res[tmp_s] = S[tmp_s]
    i += 1

for j in range(M):
    tmp_t = int(ans/M) * j
    if res[tmp_t] != 0:
        if T[j] != res[tmp_t]:
            ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
N,M = map(int,input().split())
S = input()
T = input()
if N>=M:
  if N%M!=0:
    x = fractions.gcd(N,M)
    if S[0]==T[0]:
      print (int(N*M/x))
    else:
      print (-1)
  else:
    x = int(N/M)
    a = []
    b = []
    for i in range(M):
      a.append(S[i*x])
    for i in range(M):
      b.append(T[i])
    if a==b:
      print (M)
    else:
      print (-1)
else:
  if M%N!=0:
    if S[0]==T[0]:
      x = fractions.gcd(N,M)
      print (int(M*N/x))
    else:
      print (-1)
  else:
    x = int(M/N)
    a = []
    b = []
    for i in range(N):
      a.append(T[i*x])
    for i in range(N):
      b.append(S[i])
    if a==b:
      print (N)
    else:
      print (-1)

Submission status: Wrong Answer

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)

renl=lcm(n,m)
if renl!=n and renl!=m:
    print(renl)
    exit(0)
if n>=m:
    mx=s
    mn=t
else:
    mx=t
    mn=s
lmn=len(mn)
for i in range(lmn):
    if mx[i*renl//lmn]!=mn[i]:
        print('-1')
        exit(0)
print(renl)

Submission status: Wrong Answer

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

s = len(S)
t = len(T)
if len(S) % len(T) == 0:
    for i in range(s//t):
        if T[i] != S[i*(s//t)]:
            print("-1")
            exit(0)
    print(s)
    exit(0)

elif len(T) % len(S) == 0:
    for i in range(t//s):
        if T[i*(t//s)] != S[i]:
            print("-1")
            exit(0)
    print(t)
    exit(0)

elif s == t:
    if S == T:
        print("-1")
    else:
        print(s)
    exit(0)


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


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

else:
    g = gcd(s,t)
    print(s*(t//g))

Submission status: Wrong Answer

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

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

div = gcd(n,m)
length = div * (n//div) * (m//div)

difs = length // n
dift = length // m


g = gcd(difs,dift)
gd = g * (difs//g) * (dift//g)
moves = gd//difs
movet = gd//dift

check = True
for i in range(length//div):
    if i*moves < n and i*movet < m and s[i*moves] != t[i*movet]:
        check = False
        break
if check:
    print(length)
else:
    print('-1')

Submission status: Wrong Answer

Solution code:
import fractions
a = list(map(int,input().split()))

b = list(input())

c = list(input())

d = fractions.gcd(a[0],a[1])
d = (a[0] * a[1])//d
p = 0
for i in range(0,d,min(a)):
    if i == 0:
        if b[0] != c[0]:
            p = 1
            break
    elif (i+1) // a[0] == 0 and (i+1) // a[1] == 0:
        if b[(i+1) // a[0]] != c[(i+1) // a[1]]:
            p = 1
            break


if p == 0:
    if d != max(a):
        print(d)
    else:
        print(-1)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
ai = lambda: list(map(int,input().split()))

from fractions import gcd
n,m = ai()
s = input()
t = input()
if n < m:
    n,m = m,n
    s,t = t,s
if n%m == 0:
    a = n//m
    if s[::a] == t:
        print(n)
    else:
        print(-1)
else:
    if s[0] == t[0]:
        print(n*m//gcd(n,m))
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

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

x=gcd(n,m)

if x==1:
    print(n*m)
elif x==n:
    for i in range(0,m,n):
        if t[i]!=s[i//n]:
            print(-1)
            sys.exit()
    print(n*m//x)
elif x==m:
    for i in range(0,n,m):
        if s[i]!=t[i//m]:
            print(-1)
            sys.exit()
    print(n*m//x)
else:
    if s[-1]!=t[-1] or s[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)

Submission status: Wrong Answer

Solution code:
import fractions


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


def solve(n, m, s, t):
    # assume n <= m
    if n > m:
        n, m = m, n
        s, t = t, s
    le = lcm(n, m)
    if m % n == 0:
        x = ["-"] * le
        for i, c in enumerate(t):
            x[le // m * i] = c
        for i, c in enumerate(s):
            target_index = le // n * i
            if x[target_index] == "-" or x[target_index] == c:
                x[target_index] = c
            else:
                return None
    else:
        if s[0] != t[0]:
            return None
    return le


def main():
    n, m = (int(z) for z in input().split())
    s = input()
    t = input()
    res = solve(n, m, s, t)
    if res is None:
        print(-1)
    else:
        print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import fractions
N,M = map(int,input().split())
S = input()
T = input()
if N>=M:
  if N%M!=0:
    x = fractions.gcd(N,M)
    print (int(N*M/x))
  else:
    x = int(N/M)
    a = []
    b = []
    for i in range(M):
      a.append(S[i*x])
    for i in range(M):
      b.append(T[i])
    if a==b:
      print (M)
    else:
      print (-1)
else:
  if M%N!=0:
    x = fractions.gcd(N,M)
    print (int(M*N/x))
  else:
    x = int(M/N)
    a = []
    b = []
    for i in range(N):
      a.append(T[i*x])
    for i in range(N):
      b.append(S[i])
    if a==b:
      print (N)
    else:
      print (-1)

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

def cmp(x, y):
	n = len(x)/len(y)
	for i in range(len(x)):
		if i%n == 0 and x[i] != y[int(i/n)]:
			return False
	return True

if N >= M:
	if N%M == 0 and not cmp(S,T):
		print(-1)
		sys.exit()
else:
	if M%N == 0 and not cmp(T,S):
		print(-1)
		sys.exit()

print(lcm(N,M))

Submission status: Wrong Answer

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

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

g = lcm(n,m)

if g == n or g == m:
    if n < m:
        sm = n
        ls = s
        bs = t
    else:
        sm = m
        ls = t
        bs = s
    a = g//sm
    flg = False
    for i in range(sm):
        if bs[i * a] != ls[i]:
            flg = True
            break
    if flg:
        print(-1)
    else:
        print(g)
else:
    print(g)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=len(input())
T=len(input())
print ("-1")

Submission status: Wrong Answer

Solution code:
import numpy as np,math,itertools

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

n,m=map(int,input().split())
s=input()
t=input()
nl=[]
lc=n*m
qn,qm=-1,-1
r=True

for i in range(lc):
   if i%m==0:nl+=[s[i//m]]
   else:nl+=[""]
   if i%n==0:
      print(i,i//n)
      if nl[i]!=t[i//n] and nl[i]!="":r=False

print([-1,lc][int(r)])
print(nl)

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

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

x=gcd(n,m)

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

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
print(n*2 if n*2==m*3 else n*3 if (n*2)-3==m*3 else -1)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=len(input())
T=len(input())
ans=S*T
print (ans)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=len(input())
T=len(input())
ans=S*T
print ("-1")

Submission status: Wrong Answer

Solution code:
import sys

def gcd(n,m):
    if m==0:
        return n
    else:
        return gcd(m,n%m)

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

x=gcd(n,m)

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

Submission status: Wrong Answer

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()
gcd = gcd(N, M)

if gcd == 1:
    print(N*M)
else:
    sl = [i for i in range(0, N, N//gcd)]
    tl = [i for i in range(0, M, M//gcd)]
    for i in range(len(sl)):
        if S[sl[i]] != T[tl[i]]:
            print(-1)
            break
    else:
        print(N*M//gcd)

Submission status: Wrong Answer

Solution code:
import math

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

N, M = list(map(int, input().split()))
S = input()
T = input()
g = gcd(N, M)
lcm = N*M//g
if N%M==0 or M%N==0:
    exist = True
    if N>M:
        for i in range(M):
            if S[N//M*i]!=T[i]:
                exist = False
                break
    else:
        for i in range(N):
            if T[M//N*i]!=S[i]:
                exist = False
                break
    if exist:
        print(max(N, M))
    else:
        print(-1)
else:
    print(lcm)

Submission status: Wrong Answer

Solution code:
import fractions
a = list(map(int,input().split()))

b = list(input())

c = list(input())

d = fractions.gcd(a[0],a[1])
d = (a[0] * a[1])//d
p = 0
for i in range(d):
    if i == 0:
        if b[0] != c[0]:
            p = 1
            break
    elif i // a[0] == 0 and i // a[1] == 0:
        if b[i // a[0]] != c[i // a[1]]:
            p = 1
            break


if p == 0:
    print(d)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
print(n*2 if n*2==m*3 else n*3 if n*2-3==m*3 else m[3] if n*2==m*3-2 else -1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

if n % m != 0 and m % n != 0:
	print(n // gcd(n, m) * m)
elif n >= m:
	ok = True
	for i in range(m):
		if s[(n // m) * i] != t[i]:
			ok = False
			break
	if ok:
		print(n)
	else:
		print(-1)
else:
	ok = True
	for i in range(n):
		if t[(m // n) * i] != s[i]:
			ok = False
			break
	if ok:
		print(m)
	else:
		print(-1)

Submission status: Wrong Answer

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

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

if x != N and x != M:
    print(x)
else:
    print(-1)

Submission status: Wrong Answer

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

s = len(S)
t = len(T)
if len(S) % len(T) == 0:
    for i in range(s//t):
        if T[i] != S[i*(s//t)]:
            print("-1")
            exit(0)
    print(s)
    exit(0)

if len(T) % len(S) == 0:
    for i in range(t//s):
        if T[i*(t//s)] != S[i]:
            print("-1")
            exit(0)
    print(t)
    exit(0)


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


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

else:
    g = gcd(s,t)
    print(s*(t//g))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
print(n*2 if n*2==m*3 else n*3 if n*2-3==m*3 else m[3] if n*2==m*3-3 else -1)

Submission status: Wrong Answer

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

s = len(S)
t = len(T)
if len(S) % len(T) == 0:
    for i in range(s//t):
        if T[i] != S[i*(s//t)]:
            print("-1")
            exit(0)
    print(t)
    exit(0)

if len(T) % len(S) == 0:
    for i in range(t//s):
        if T[i*(t//s)] != S[i]:
            print("-1")
            exit(0)
    print(s)
    exit(0)


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


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

else:
    g = gcd(s,t)
    print(s*(t//g))

Submission status: Wrong Answer

Solution code:
ai = lambda: list(map(int,input().split()))

from fractions import gcd
n,m = ai()
s = input()
t = input()
if n < m:
    n,m = m,n
    s,t = t,s
if n%m == 0:
    a = n//m
    if all(s[a*i] == t[i] for i in range(m)):
        print(n)
    else:
        print(-1)
else:
    if s[0] == t[0]:
        print(n*m//gcd(n,m))
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10000)


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


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


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


@mt
def slv(N, M, S, T):
    if N < M:
        S, T = T, S
        N, M = M, N

    if N % M == 0:
        for i in range(M):
            if S[(N//M) * i] != T[i]:
                return -1
        else:
            return N
    else:
        if S[0] != T[0]:
            return -1
        return lcm(N, M)


def main():
    N, M = read_int_n()
    S = read_str()
    T = read_str()
    print(slv(N, M, S, T))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import fractions


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


def solve(n, m, s, t):
    # assume n <= m
    if n > m:
        n, m = m, n
        s, t = t, s
    le = lcm(n, m)
    if m % n == 0:
        x = ["-"] * le
        for i, c in enumerate(t):
            x[le // m * i] = c
        for i, c in enumerate(s):
            target_index = le // n * i
            if x[target_index] == "-" or x[target_index] == c:
                x[target_index] = c
            else:
                return None
    return le


def main():
    n, m = (int(z) for z in input().split())
    s = input()
    t = input()
    res = solve(n, m, s, t)
    if res is None:
        print(-1)
    else:
        print(res)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

n, m = map(int, input().split())
s = input()
t = input()
if max(n, m) % min(n, m) == 0:
    print(-1)
else:
    print(lcm(n, m))

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
print(n*2 if n*2==m*3 else n*3 if n*2-3==m*3 else -1)

Submission status: Wrong Answer

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:
    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: Wrong Answer

Solution code:
N, M = [int(i) for i in input().split(' ')]
S, = [i for i in input().split(' ')]
T, = [i for i in input().split(' ')]
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

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

if S[0] != T[0]:
    print(-1)
else:
    print(lcm(N,M))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
s=input() ;a=len(s)
t=input() ;b=len(t)
def gcd(a,b):
    if b==0: return a
    else:
        return gcd(b,a%b)
def lcm(a,b):
    return a*b//gcd(a,b)
L=lcm(len(s),len(t))

A=["" for i in range(L)]

now=0
for i in range(0,L,L//a):
    A[i]=s[now]
    now+=1
now=0
#print(A)
for i in range(0,L,L//b):
    if A[i]!="" and A[i]!=t[now]:
        print(-1);exit()
    A[i]=t[now]
    now+=1
print(L)

Submission status: Time Limit Exceeded

Solution code:
import sys

# A - Two Abbreviations
def get_lcm(a, b):
	import math
	return (a * b) // math.gcd(a, b)


def is_match(X, L):
  global M
  global T

  i = 0
  m = L//M * i

  while m + 1 < L:
    if m+1 in X.keys():
      if X[m+1] != T[i]:
        return False

    i += 1
    m = L//M * i

  return True


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

if S[0] != T[0]:
  print(-1)
else:
  lcm = get_lcm(N, M)

  for i in range(1, 100):
    L = lcm * i
    X = dict()

    j = 0
    n = L//N * j

    while n + 1 < L:
      X[n+1] = S[j]

      j += 1
      n = L//N * j

    if is_match(X, L):
      print(L)
      exit()

  print(-1)

Submission status: Time Limit Exceeded

Solution code:
from math import gcd
N,M=list(map(int, input().split()))
S=input()
T=input()
g=gcd(N,M)
l=N*M//g
n=l//N
m=l//M
for i in range(l):
  if i%n==0 and i%m==0:
    if S[i//n]!=T[i//m]:
      print(-1)
      exit()
else:
  print(l)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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)
d = {}
s = list()
# s = set()
for i in range(N):
    d[str(i * (L // N))] = S[i]
    s.append(str(i * (L // N)))
    # s.add(str(i * (L // N)))

for i in range(M):
    key = str(i * (L // M))
    if key in s:
        if d[key] != T[i]:
            print(-1)
            exit()
print(L)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]
b2 = set(b0)
b3 = set(b1)

for j, i in enumerate(b0):
    if i in b3:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
            
for j, i in enumerate(b1):
    if i in b2:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)

Submission status: Time Limit Exceeded

Solution code:
import math
import sys
n, m = map(int, input().split())
s = list(input())
t = list(input())
a = (n*m)//math.gcd(n, m)
b0 = [i*(a//n)+1 for i in range(n)]
b1 = [i*(a//m)+1 for i in range(m)]

for j, i in enumerate(b0):
    if i in b1:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
    
for j, i in enumerate(b1):
    if i in b0:
        if s[b0.index(i)] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)

Submission status: Time Limit Exceeded

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

Length = N*M//math.gcd(N,M)
Flag   = True
SIndex = [Nk*(Length//N) for Nk in range(0,N)]
SList  = list(S)
    
for Mk in range(0,M):
    if Mk*(Length//M) in SIndex:
        MInd = SIndex.index(Mk*(Length//M))
        if SList[MInd]!=T[Mk]:
            Flag = False
            break
        
if Flag:
    print(Length)
else:
    print(-1)

Submission status: Time Limit Exceeded

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, M = mapint()
S = list(input())
T = list(input())
from math import gcd

leng = N*M//gcd(N, M)

for i in range(1, leng+1):
    if (i-1)%(leng//N)==0 and(i-1)%(leng//M)==0:
        if S[(i-1)//(leng//N)]!=T[(i-1)//(leng//M)]:
            print(-1)
            break
else:
    print(leng)

Submission status: Time Limit Exceeded

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

if M > N:
	M, N = N, M
	S, T = T, S

import math
G = N*M//math.gcd(N, M)

A = []
B = []

for i in range (0, N):
	A.append(1+i*G//N) 
for i in range (0, M):
	B.append(1+i*G//M)

import bisect
FA = []
FB = []

for i in range (0, M):
	if A.count(B[i]) != 0:
		FA.append(A.index(B[i]))
		FB.append(i)
        
for i in range (0, len(FA)):
	if S[FA[i]] != T[FB[i]]:
		print(-1)
		exit()
print(G)

Submission status: Time Limit Exceeded

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

    L = (N * M) // math.gcd(N, M)
    good = [-1 for i in range(4 * L)]

    idx_s = 0
    idx_t = 0

    for i in range(1, max(N, M) + 1):
        idx_s = (i - 1) * (L // N) + 1
        idx_t = (i - 1) * (L // M) + 1

        if N < M:

            if i <= N:
                good[idx_s] = S[i - 1]

            if i == 1:
                if S[i - 1] != T[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_t] == -1 or good[idx_t] == T[i - 1]:
                    good[idx_t] = T[i - 1]
                else:
                    print(-1)
                    exit()

        else: #N >= M

            if i <= M:
                good[idx_t] = T[i - 1]

            if i == 1:
                if T[i - 1] != S[i - 1]:
                    print(-1)
                    exit()
            else:
                if good[idx_s] == -1 or good[idx_s] == S[i - 1]:
                    good[idx_s] = S[i - 1]
                else:
                    print(-1)
                    exit()

    max_idx = max(((N - 1) * (L // N) + 1), ((M - 1) * (L // M) + 1))
    r, m = divmod(max_idx, L)

    ans = 0
    if m == 0:
        ans = r * L
    else:
        ans = (r + 1) * L

    print(ans)

if __name__ == "__main__":
    main3()

Submission status: Time Limit Exceeded

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

    L = (N * M) // math.gcd(N, M)
    good = [-1 for i in range(4 * L)]

    idx_s = 0
    idx_t = 0

    idx = 0
    for i in range(1, N + 1):
        idx = (i - 1) * (L // N) + 1
        good[idx] = S[idx_s]
        idx_s += 1

    idx = 0
    for i in range(1, M + 1):
        idx = (i - 1) * (L // M) + 1

        if i == 1:
            if good[idx] != T[idx_t]:
                print(-1)
                exit()
            else:
                idx_t += 1

        else:
            if good[idx] == -1 or good[idx] == T[idx_t]:
                good[idx] = T[idx_t]
                idx_t += 1
            else:
                print(-1)
                exit()
    
    max_idx = max(((N - 1) * (L // N) + 1), ((M - 1) * (L // M) + 1))
    r, m = divmod(max_idx, L)

    ans = 0
    if m == 0:
        ans = r * L
    else:
        ans = (r + 1) * L

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import math

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


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

l = (lcm(n, m))
# print(l)

slst = []
for i in range(n):
    num = i * l // n + 1
    slst.append(num - 1)
tlst = []
for i in range(m):
    num = i * l // m + 1
    tlst.append(num - 1)

# print(slst)
# print(tlst)

x = [0 for _ in range(l)]
idx = 0
for i in slst:
    x[i] = s[idx]
    idx += 1
# print(x)

idxt = 0
for i in tlst:
    if x[i] != 0:
        if x[i] != t[idxt]:
            print(-1)
            exit()
    idxt += 1
print(l)

Submission status: Time Limit Exceeded

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