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:
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

#a,bの最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)
n,m = map(int,input().split())
s = input()
t = input()
mx = max(n,m)
mn = min(n,m)
if mx / mn % 1 == 0:
    c = mx//mn
    if mn == len(s):
        if s == t[::c]:
            print(mx)
        else:
            print(-1)
    else:
        if t == s[::c]:
            print(mx)
        else:
            print(-1)
else:
    num = lcm(n,m)
    mmx = num // mx
    mmn = num // mn
    ber = lcm(mmx,mmn)
    if ber < mn:
        if s[::ber] == t[::ber]:
            print(num)
        else:
            print(-1)
    else:
        print(num)

Submission status: Wrong Answer

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

#a,bの最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)
n,m = map(int,input().split())
s = input()
t = input()
mx = max(n,m)
mn = min(n,m)
if mx / mn % 1 == 0:
    c = mx//mn
    if mn == len(s):
        if s == t[::c]:
            print(mx)
        else:
            print(-1)
    else:
        if t == s[::c]:
            print(mx)
        else:
            print(-1)
else:
    num = lcm(n,m)
    mmx = num // mx
    mmn = num // mn
    ber = lcm(mmx,mmn)
    if s[::ber] == t[::ber]:
        print(num)
    else:
        print(-1)

Submission status: Wrong Answer

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

#a,bの最小公倍数
def lcm(a, b):
	return a * b // gcd (a, b)
n,m = map(int,input().split())
s = input()
t = input()
mx = max(n,m)
mn = min(n,m)
if mx / mn % 1 == 0:
    c = mx//mn
    if mn == len(s):
        if s == t[::c]:
            print(mx)
        else:
            print(-1)
    else:
        if t == s[::c]:
            print(mx)
        else:
            print(-1)
else:
    if s[0] == t[0]:
        print(lcm(n,m))
    else:
        print(-1)

Submission status: Wrong Answer

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

# 良い文字列が作れるなら、答えはLCM(n, m)となる
gcd = math.gcd(n, m)
print(s[::n//gcd] ,t[::m//gcd])
if s[::n//gcd] == t[::m//gcd]:
    print(n * m // gcd)
else:
    print(-1)

Submission status: Wrong Answer

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

ans = int((n*m)/math.gcd(n,m))
g = math.gcd(n,m)
for i in range(g):
  print (i*(n//g),i*(m//g))
  if s[i*(n//g)] != t[i*(m//g)]:
    print (-1)
    exit()
print (ans)

Submission status: Wrong Answer

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

ans = int((n*m)/math.gcd(n,m))
d = defaultdict(int)
if n > m:
  for i in range(n):
    if i == 0:
      d[1] = s[i]
    else:
      d[i*int(ans/n)+1] = s[i]
  ng = 0
  for i in range(m):
    if i == 0:
      if d[1] != t[i]:
        ng += 1
    else:
      if d[i*int(ans/m)+1] != 0 and d[i*int(ans/m)+1] != t[i]:
        print (i,t[i])
        ng += 1
else:
  for i in range(m):
    if i == 0:
      d[1] = t[i]
    else:
      d[i*int(ans/m)+1] = t[i]
  ng = 0
  for i in range(n):
    if i == 0:
      if d[1] != s[i]:
        ng += 1
    else:
      if d[i*int(ans/n)+1] != 0 and d[i*int(ans/n)+1] != s[i]:
        ng += 1
if ng > 0:
  print (-1)
else:
  print (ans)

Submission status: Wrong Answer

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 = []
    for i in range(N):
        j = XdivS*i
        X.append((j, S[i]))
    X.sort(key=lambda x: x[0], reverse=True)
    
    k, x = X.pop()
    for i in range(M):
        j = XdivT*i
        for a in range(lengthX):
            if len(X) == 0:
                print(lengthX)
                return
            if k < j:
                k, x = X.pop()
            else:
                break
        if k == j:
            if x != T[i]:
                print(-1)
                return
    print(lengthX)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

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

lcm = get_lcm(N, M)

for i in range(1, 1000):
  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: Wrong Answer

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

lcm = get_lcm(N, M)

for i in range(1, 10000):
  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: Wrong Answer

Solution code:
from math import gcd
n,m=map(int,input().split())
s=input()
t=input()
res=n*m//gcd(n,m)
if n<res and m<res:
  print(res)
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
from math import gcd
n,m = map(int,input().split())
z = n * m // gcd(n,m)
s = input()
t = input()
n = z // n
m = z // m
x = 0
y = 0
i = 0
j = 0
while x + 1 < z and y + 1 < z:
    if x > y:
        y += m
        j += 1
    elif x < y:
        x += n
        i += 1
    else:
        if s[i] != t[j]:
            print(-1)
            exit(0)
        x += n
        y += m
        i += 1
        j += 1
print(z)

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n
while a % m != 0:
    a += n
    if a >= 10 ** 9:
        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: Wrong Answer

Solution code:
n,m = map(int,input().split())
a = n
while a % m != 0:
    a += n
    if a >= 10 ** 8:
        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: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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)
    elif n % m != 0 and m % n != 0:
        r = (n * m) // gcd(n, m)
        print(r)
    else:
        if n > m:
            l1 = n
            l2 = m
            s1 = s
            s2 = t
        else:
            l1 = m
            l2 = n
            s1 = t
            s2 = s
        ld = l1 // l2
        for i1 in range(1, l2):
            if s2[i1] != s1[i1*ld]:
                print(-1)
                sys.exit()
        print(l1)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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 = set([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[list(b1).index(i)]:
            print(-1)
            sys.exit()    
print(a)

Submission status: Wrong Answer

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 set(b1):
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
    
for j, i in enumerate(b1):
    if i in set(b0):
        if s[j] != t[b1.index(i)]:
            print(-1)
            sys.exit()
print(a)

Submission status: Wrong Answer

Solution code:
import math
n,m=map(int,input().split())
s=input()
t=input()
lcm=n*m//math.gcd(n,m)
print(-1 if (lcm==n and s!=t*(n//m)) or (lcm==m and t!=s*(m//n)) or s[0]!=t[0] else lcm)

Submission status: Wrong Answer

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
is_ok = True

s_inds = [i * d_s for i in range(n)]
t_inds = [i * d_t for i in range(m)]

j = 0
for i in range(n):
    if s_inds[i] == t_inds[j]:
        if s[s_inds[i]] != t[j]:
            is_ok = False
            break
        else:
            j += 1
    if j == m:
        break

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

Submission status: Wrong Answer

Solution code:
from math import gcd


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


def solve(l, s) -> int:
    if l[0] != s[0]:
        return -1
    if len(l) % len(s) == 0:
        m = len(l) // len(s)
        f = True
        for i in range(len(s)):
            f &= (s[i] == l[m * i])
        if f:
            return len(l)
        else:
            return -1
    else:
        return lcm(len(l), len(s))


n, m = map(int, input().split())
s = input()
t = input()
ans = 0
if m > n:
    ans = solve(t, s)
elif n > m:
    ans = solve(s, t)
else:
    if s == t:
        ans = n
    else:
        ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
from math import gcd


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


def solve(l, s) -> int:
    if len(l) % len(s) == 0:
        m = len(l) // len(s)
        f = True
        for i in range(len(s)):
            f &= (s[i] == l[m * i])
        if f:
            return len(l)
        else:
            return -1
    else:
        return lcm(len(l), len(s))


n, m = map(int, input().split())
s = input()
t = input()
ans = 0
if m > n:
    ans = solve(t, s)
elif n > m:
    ans = solve(s, t)
else:
    if s == t:
        ans = n
    else:
        ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(a, b):
    _a = a
    while a % b != 0:
        a += _a
    return a


_N, _M = map(int, input().split())
N = max(_N, _M)
M = min(_N, _M)
S = input()
T = input()
L = gcd(N, M)
#print(L)
s_now = 0
t_now = 0
while True:
    if S[s_now] != T[t_now]:
        print(-1)
        break
    s_now += L // M
    t_now += L // N
    if s_now >= N or t_now >= M:
        print(L)
        break

Submission status: Wrong Answer

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

print((N*M)//g)

Submission status: Wrong Answer

Solution code:
import math
N, M = map(int, input().split())
S = list(input())
T = list(input())
lcm = (N * M) // math.gcd(N, M)
ans = lcm
Sp = lcm//N
Tp = lcm//M
lcmlcm = (Sp * Tp) // math.gcd(Sp, Tp)
owari = min(Sp*N, N*Tp)
for i in range(0, owari, lcmlcm):
    if S[i//Sp] != T[i//Tp]:
        ans = -1
        break
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

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

if n == 1:
    if s[0] == t[0]:
        print(m)
    else:
        print(-1)
    exit()

lcm = n * m // math.gcd(n, m)

n1 = lcm//n
m1 = lcm//m

lcm2 = n1 * m1 // math.gcd(n1, m1)
stepn = lcm2//n1
stepm = lcm2//m1

for i in range(1,10**5):
    if stepn*i > n-1 or stepm*i > m-1:
        print(lcm)
        break
    if s[stepn*i] != t[stepm*i]:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
import math

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

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

lcm = n * m // math.gcd(n, m)

n1 = lcm//n
m1 = lcm//m

lcm2 = n1 * m1 // math.gcd(n1, m1)
stepn = lcm2//n1
stepm = lcm2//m1

for i in range(1,10**5):
    if stepn*i > n-1 or stepm*i > m-1:
        print(lcm)
        break
    if s[stepn*i] != t[stepm*i]:
        print(-1)
        exit()

Submission status: Wrong Answer

Solution code:
import math

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

lcm = n * m // math.gcd(n, m)

n1 = lcm//n
m1 = lcm//m

lcm2 = n1 * m1 // math.gcd(n1, m1)
stepn = lcm2//n1
stepm = lcm2//m1

for i in range(1,10**5):
    if stepn*i > n-1 or stepm*i > m-1:
        print(lcm)
        break
    if s[stepn*i] != t[stepm*i]:
        print(-1)
        exit()

Submission status: Wrong Answer

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

if S[0] != T[0]:
    print(-1)
    exit()
if N%M == 0 or M%N == 0:
    num = max(N,M)//min(N,M)
    for i in range(1,min(N,M)):
        if N < M:
            if S[i] != T[num*i]:
                print(-1)
                exit()
        else:
            if T[i] != S[num*i]:
                print(-1)
                exit()
    print(max(N,M))
    exit()
print(lcm_base(N, M))

Submission status: Wrong Answer

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

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

G=gcd(n,m)
a,b = n//G,m//G
s=s[1::a]
t=t[1::b]
if s==t: print(a*b*G)
else: print(-1)

Submission status: Wrong Answer

Solution code:
from math import gcd

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

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

Submission status: Wrong Answer

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)

if l > N and l > M:
    print(l)
else:
    print(-1)

Submission status: Wrong Answer

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

from math import*
ans=N*M//gcd(N,M)

if N%M==0:
    i=0
    j=0
    while i<M and j<N:
        if T[i]!=S[j]:
            ans=-1
        i+=1
        j+=ans//M

if M%N==0:
    i=0
    j=0
    while i<N and j<M:
        if T[i]!=S[j]:
            ans=-1
        i+=1
        j+=ans//N

if N==M:
    if S!=T:
        ans=-1

print(ans)

Submission status: Wrong Answer

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
x = l//n
y = l//m
g = lcm(x,y)
a = 0

while len(s)>= g and len(t)>= g:
    if s[a-1] != t[a-1]:
        print(-1)
        sys.exit()
    a += g

print(l)

Submission status: Wrong Answer

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n,m = I()
s = v()
t = v()
l = lcm(n,m)
x = l//n
y = l//m
g = lcm(x,y)
a = 0

while len(s)-1 >= g and len(t)-1 >= g:
    if s[a-1] != t[a-1]:
        print(-1)
        sys.exit()
    a += g

print(l)

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    import math
    N, M = [int(item) for item in input().split()]
    S = input()
    T = input()

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

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

    cycle_s = res // N
    cycle_t = res // M
    cycle_overlap = cycle_s * cycle_t // math.gcd(cycle_s, cycle_t)

    for i in range(res//cycle_overlap):        
        if T[i*cycle_overlap//cycle_t] == S[i*cycle_overlap//cycle_s]:
            pass
        else:
            res = -1
            break

    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
    '''
    code here
    '''
    import math
    N, M = [int(item) for item in input().split()]
    S = input()
    T = input()

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

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

    for i in range(M):
        if i % (res//N) == 0 and i* (res//M) < N:
            if T[i] == S[i*res//M]:
                pass
            else:
                res = -1
                break

    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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)
print(-1)
exit()
x = [""] * (L)
for i in range(N):
    idx = i * (L // N)
    x[idx] = S[i]
for i in range(M):
    idx = i * (L // M)
    if x[idx] != "" and x[idx] != T[i]:
        print(-1)
        exit()
print(L)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n, m = [int(x) for x in input().split()]
    s = input()
    t = input()
    l = n * m // gcd(n, m)
    if l in [n, m]:
        if n > m:
            n, m = m, n
            s, t = t, s
        ok = 1
        for i in range(n):
            if s[i] != t[i * l // n]:
                ok = 0
        if ok:
            print(l)
        else:
            print(-1)
    else:
        print(l)

    




        
    

    
    

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""

2 2 2 2 2

"""

Submission status: Wrong Answer

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

snum=[i*g/n+1 for i in range(n)]
tnum=[i*g/m+1 for i in range(m)]
print(snum,tnum)

if n<m:
    s,t=t,s
    snum,tnum=tnum,snum
for i in range(len(snum)):
    if snum[i] in tnum:
        if s[i]!=t[tnum.index(snum[i])]:
            print(-1)
            exit()
print(g)

Submission status: Wrong Answer

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
g=gcd(a,b)
if a%b==0 or b%a==0:
    for i in range(g):
        if s[a//g*i]!=t[b//g*i]:
            print(-1)
            exit()
if t[0]!=s[0]:
    print(-1)
    exit()
print(a*b//g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
g=gcd(a,b)
if a%b==0 or b%a==0:
    for i in range(g):
        if s[a//g*i]!=t[b//g*i]:
            print(-1)
            exit()
if t[0]!=s[0]:
    print(-1)
    exit()
print(a*b*g)

Submission status: Wrong Answer

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
ans=a*b//gcd(a,b)
if a%b==0:
    if not all(t[i]==s[i*a//b] for i in range(b)):ans=-1
if b%a==0:
    if not all(s[i]==t[i*b//a] for i in range(a)):ans=-1
if s[0]!=t[0]:
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
print(-1)

Submission status: Wrong Answer

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
ans=a*b//gcd(a,b)
if a%b==0:
    if not all(s[i]==t[i*a//b] for i in range(b)):ans=-1
if b%a==0:
    if not all(t[i]==s[i*b//a] for i in range(a)):ans=-1
if s[0]!=t[0]:
    ans=-1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import *
a,b=map(int,input().split())
s=input()
t=input()
ans=a*b//gcd(a,b)
# if a%b==0:
#     if not all(s[i]==t[1+i*a//b] for i in range(b)):ans=-1
# if b%a==0:
#     if not all(t[i]==s[1+i*b//a] for i in range(a)):ans=-1
print(ans)

Submission status: Wrong Answer

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

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


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


ans = lcm(n, m)
if ans in (n, m) and s != t:
    print(-1)
else:
    print(ans)

Submission status: Wrong Answer

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

lenS = len(S)
lenT = len(T)


def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x


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


lcmnum = lcm(lenS, lenT)

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

if S == T:
    print(lenS)
    exit()

if lcmnum == lenS or lcmnum == lenT:
    print(-1)
else:
    print(lcmnum)

Submission status: Wrong Answer

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

lenS = len(S)
lenT = len(T)


def gcd(x, y):
    while y != 0:
        x, y = y, x % y
    return x


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


lcmnum = lcm(lenS, lenT)

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

if lcmnum == lenS or lcmnum == lenT:
    print(-1)
else:
    print(lcmnum)

Submission status: Wrong Answer

Solution code:
from math import gcd
n, m, S, T = open(0).read().split()
n, m = int(n), int(m)
l = n*m // gcd(n, m)
try:
    A = ['*'] * l
    t = l//n
    for i in range(n):
        A[i*t] = S[i]
    t = l//m
    for i in range(m):
        if A[i*t] == T[i] or A[i*t] == '*':
            A[i*t] = T[i]
        else:
            print(-1)
            break
    else:
        print(l)
except:
    print(l)

Submission status: Wrong Answer

Solution code:
import math
n,m=map(int,input().split())
s=input()
t=input()
g=n*m//math.gcd(n,m)
if s[0]!=t[0]:
    print(-1)
    exit()
a,b=max(n,m),min(n,m)
if a%b==0:
    if s>t:
        if s[::a//b]==t:
            print(g)
        else:
            print(-1)
    else:
        if t[::a//b]==s:
            print(g)
        else:
            print(-1)
else:
    print(g)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

N, M = map(int,input().split())
S = input()
T = input()
L = (N*M) // gcd(N,M)
S_a = L // N
T_a = L // M

i = 0
j = 0
while True:
    if S[i] != T[j] and i == j:
        print('-1')
        break
    i += S_a
    j += T_a
    if i >= N or j >= M:
        print(L)
        break

Submission status: Wrong Answer

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

g=gcd(N,M)

if g==1:
  ans=(N*M)//g
else:
  n=N//g
  m=M//g
  import numpy as np
  pn=np.arange(m, M, m)
  qm=np.arange(n, N, n)
  ans=(N*M)//g
  for i in range(len(pn)):
    if s[qm[i]]!=t[pn[i]]:
      ans=-1
      break
print(ans)

Submission status: Wrong Answer

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

g=gcd(N,M)

if g==1:
  ans=(N*M)//g
else:
  n=N//g
  m=M//g
  import numpy as np
  pn=np.arange(m, N, m)
  qm=np.arange(n, M, n)
  ans=(N*M)//g
  for i in range(len(pn)):
    if s[pn[i]]!=t[qm[i]]:
      ans=-1
      break
print(ans)

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

Submission status: Wrong Answer

Solution code:
import math

n, m = map(int, input().split())
s = input()
t = input()
if n > m:
    l_str = s
    s_str = t
    ma_nm = n
    mi_nm = m
elif n < m:
    l_str = t
    s_str = s
    ma_nm = m
    mi_nm = n
else:
    if s == t:
        ans = n
    else:
        ans = -1
    print(ans)
    exit()

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

if ma_nm % mi_nm == 0:
    div = ma_nm // mi_nm
    ans = ma_nm
    i = 0
    while i < mi_nm:
        if s_str[i] != l_str[i*div]:
            ans = -1
            break
        i += 1
else:
    ans = lcm(n, m)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N,M = I()
S = list(s())
T = list(s())
if math.gcd(N,M) == 1:
    print(N*M)
    exit()
n = lcm(N,M)
l = (N-1)//(n//M)
for i in range(l+1):
    if S[i*(n//M)] != T[i*(n//N)]:
        print(-1)
        exit()
print(n)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

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

    if N%M != 0:
        if S[0] == T[0]:
            ans = M*N//gcd(N,M)
        else:
            ans = -1
    else:
        x = N//M
        ans = N
        for i in range(M):
            j = i*x
            if S[j] != T[i]:
                ans = -1
                break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

from math import radians,cos,sin,tan,ceil,floor,log,factorial,gcd,pi,hypot
def main():
    N,M = map(int,input().split())
    S = input()
    T = input()

    if N%M != 0 and M%N != 0:
        ans = N*M//gcd(N,M)
    else:
        if N < M:
            N,M = M,N
            S,T = T,S
        x = N//M
        ans = N
        for i in range(M):
            j = i*x
            if S[j] != T[i]:
                ans = -1
                break
    print(ans)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(str, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N, M = MAP()

S = input()
T = input()

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

G = gcd(N, M)

if G == 1:
	print(N*M)
	exit()