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:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
ans=0
if n*m//fractions.gcd(n,m)==max(n,m):
    cnt=[""]*(n*m//fractions.gcd(n,m))
    for i in range(n):
        cnt[i*max(n,m)//n]=s[i]
    for j in range(m):
        if t[j]!=cnt[j*max(n,m)//m]:
            ans=-1
            break
    if ans!=-1:
        print(max(n,m))
    else:
        print(ans)
else:
    print(n*m//fractions.gcd(n,m))

Submission status: Wrong Answer

Solution code:
N,M = map(int,input().split())
def gcd(a,b):
    if a >= b:
        if b == 0:
            return a
        else:
            return gcd(b,a%b)
    else:
        if a == 0:
            return b
        else:
            return gcd(a,b%a)
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
else:
    flag = True
    if N//M >= 2:
        for i in range(2,N//M+1):
            if T[-1] != S[i*M-1]:
                flag = False
    if M//N >= 2:
        for i in range(2,M//N+1):
            if S[-1] != T[i*N-1]:
                flag = False
    if flag:
        print(int((N//gcd(N,M))*M))
    else:
        print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
import sys
input = sys.stdin.readline

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

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

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

Submission status: Wrong Answer

Solution code:
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)

L = lcm(N, M)
X = [["A"] for i in range(L)]

for i in range(N):
    index = i * (L // N)
    X[index] = S[i]

for i in range(M):
    index = i * (L // M) 
    if X[index] != "A" and X[index] != T[i]:
        print(-1)
        sys.exit()
    
print(L)

Submission status: Wrong Answer

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(p):
    if sf*i>n or tf*i>m:
        if s[i*sf-1]!=t[i*tf-1]:
            print(-1)
            exit()
    else:
        break
print(p)

Submission status: Wrong Answer

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(p):
    if sf*i>n-1 or tf*i>m-1:
        if s[i*sf-1]!=t[i*tf-1]:
            print(-1)
            exit()
    else:
        break
print(p)

Submission status: Wrong Answer

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(p):
    if sf*i>n-1 or tf*i>m-1:
        if s[i*m]!=t[i*n]:
            print(-1)
            exit()
    else:
        break
print(p)

Submission status: Wrong Answer

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
p=n*m//fractions.gcd(n,m)
sf=p//n
tf=p//m
for i in range(1,p):
    if i%n==i%m==0:
        if s[i//m]!=t[i//n]:
            print(-1)
            exit()
print(p)

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int, input().split())
S = input()
T = input()
tmp = (N * M) // fractions.gcd(N, M)
if tmp > max(N, M):
    print(tmp)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = readline()
T = readline()
S = list(S.rstrip())
T = list(T.rstrip())
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

g = gcd(N, M)
l = N * M // g
x = l // N
y = l // M
if g != 1:
    #print(S[::x])
    if S[::y] != T[::x]:
        print(-1)
        exit()
print(l)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.readline
readlines = sys.stdin.buffer.readlines

N, M = map(int, readline().split())
S = readline()
T = readline()
S = list(S.rstrip())
T = list(T.rstrip())
def gcd(a, b):
    while b:
        a, b = b, a%b
    return a

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

if g != 1:
    #print(S[::x])
    if S[::y] != T[::x]:
        print(-1)
        exit()
print(l)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p #20ms
tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
# tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
print(-1)
# ans=n*m//p
# tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
# print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
# for i in range(n):
# 	tt[i*(ans//n)]=ord(s[i])
# for i in range(m):
# 	if tt[i*(ans//m)]!=ord(t[i]) and tt[i*(ans//m)]!=0:
# 		print(-1)
# 		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
n,m=map(int,input().split())
s=input()
t=input()
p=gcd(n,m)
ans=n*m//p
tt=[0 for _ in range(ans)]
for i in range(n):
	tt[i*(ans//n)]=s[i]
for i in range(m):
	if tt[i*(ans//m)]!=t[i] and tt[i*(ans//m)]!="":
		print(-1)
		exit()
print(ans)

Submission status: Wrong Answer

Solution code:
# AGC028
from fractions import gcd
#from math import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)

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

if N == 1 or M == 1:
    if N == 1:
        print(M)
    if M == 1:
        print(N)
    exit()

if N == l:
    tmp = N // M
    for i in range(tmp):
        if S[i * tmp] == T[i]:
            pass
        else:
            print(-1)
            exit()
if M == l:
    tmp = M // N
    for i in range(tmp):
        if T[i * tmp] == S[i]:
            pass
        else:
            print(-1)
            exit()

G = gcd(N, M)
if G != 1:
    for g in range(G):
        if S[g * N // G] == T[g * M // G]:
            pass
        else:
            print(-1)
            exit()
print(l)

Submission status: Wrong Answer

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

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

if N == 1 or M == 1:
    if N == 1:
        print(M)
    if M == 1:
        print(N)
    exit()

if N == l:
    tmp = N // M
    for i in range(tmp):
        if S[i * tmp] == T[i]:
            pass
        else:
            print(-1)
            exit()
if M == l:
    tmp = M // N
    for i in range(tmp):
        if T[i * tmp] == S[i]:
            pass
        else:
            print(-1)
            exit()

G = gcd(N, M)
if G != 1:
    for g in range(G):
        if S[g * N // G] == T[g * M // G]:
            pass
        else:
            print(-1)
            exit()
else:
    print(l)

Submission status: Wrong Answer

Solution code:
# AGC028
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
N, M = map(int, input().split())
S = input()
T = input()
l = lcm(N, M)
flag = True
if S[0] != T[0]:
    print(-1)
    exit()
if N == 1:
    print(M)
    exit()
if M == 1:
    print(N)
    exit()
if N == l:
    tmp = N // M
    for i in range(tmp):
        if S[i * tmp] == T[i]:
            pass
        else:
            flag = False
            break
if M == l:
    tmp = M // N
    for i in range(tmp):
        if T[i * tmp] == S[i]:
            pass
        else:
            flag = False
            break
if flag:
    print(l)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
# AGC028
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
N, M = map(int, input().split())
S = input()
T = input()
l = lcm(N, M)
flag = True
if S[0] != T[0]:
    print(-1)
    exit()
if N == l:
    tmp = N // M
    for i in range(tmp):
        if S[i * tmp] == T[i]:
            pass
        else:
            flag = False
            break
if M == l:
    tmp = M // N
    for i in range(tmp):
        if T[i * tmp] == S[i]:
            pass
        else:
            flag = False
            break
if flag:
    print(l)
else:
    print(-1)

Submission status: Wrong Answer

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

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

# 最小公倍数を求める
if N == M:
    l = -1
    if S == T:
        l = N
else:
    if N > M:
        w_n, w_m = N, M
        w_s, w_t = S, T
    else:
        w_n, w_m = M, N
        w_s, w_t = T, S

    if w_n % w_m == 0:
        k = w_n // w_m
        is_ok = True
        l = len(w_s)
        for i, char in enumerate(w_t):
            if S[(i - 1) ** k] != char:
                is_ok = False
                break
        if not is_ok:
            l = -1
    else:
        # 最小公倍数が候補
        l = w_m * w_n // fractions.gcd(w_m, w_n)
        is_ok = True
        s_idx_list = []
        t_idx_list = []

        # w_mの方が小さいのでw_mを基準にする
        for i in range(w_m):
            s_idx_list.append(i * (l // w_n))
            t_idx_list.append(i * (l // w_m))
        common_idx_list = list(set(s_idx_list) & set(t_idx_list))

        for j in common_idx_list:
            s_idx = w_n * j // l
            t_idx = w_m * j // l
            if w_s[s_idx] != w_t[t_idx]:
                is_ok = False
                break
        if not is_ok:
            l = -1

ans = l

print(ans)

Submission status: Wrong Answer

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

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

# 最小公倍数を求める
if N == M:
    l = -1
    if S == T:
        l = N
else:
    if N > M:
        w_n, w_m = N, M
        w_s, w_t = S, T
    else:
        w_n, w_m = M, N
        w_s, w_t = T, S

    if w_n % w_m == 0:
        k = w_n // w_m
        is_ok = True
        l = len(w_s)
        for i, char in enumerate(w_t):
            if S[(i - 1) ** k] != char:
                is_ok = False
                break
        if not is_ok:
            l = -1
    else:
        # 最小公倍数が候補
        l = w_m * w_n // fractions.gcd(w_m, w_n)
        is_ok = True
        for i in range(l):
            s_idx = i * (l // w_n)
            t_idx = i * (l // w_m)
            if t_idx > w_m - 1:
                break
            if s_idx != t_idx:
                continue
            if w_s[s_idx] != w_t[t_idx]:
                is_ok = False
                break
        if not is_ok:
            l = -1

ans = l

print(ans)

Submission status: Wrong Answer

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

def gcd(x,y):
  if x==0:
    return y
  else:
    return gcd(y%x,x)

k=N*M//gcd(N,M)
X=[0]*N
for i,s in enumerate(S):
  X[i]=s
  
for i,t in enumerate(T):
  if (i*k//M)%(k//N)!=0:
    pass
  elif (i*k//M)%(k//N)==0 and X[(i*k//M)//(k//N)]:
    pass
  else:
    print(-1)
    exit()
    
print(k)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y <= 0:
        return x
    else:
        return gcd(y,x%y)

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

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

ans = -1
if N == M:
    if S == T:
        ans = N
    else:
        ans = -1
elif S[0] != T[0]:
    ans = -1
else:
    for i in range(1,max(N,M)):
        for j in range(1,max(N,M)):
            if i*(L//M)+1 < min(N,M) and j*(L//N)+1 < min(N,M) and T[i*(L//M)+1] == S[j*(L//N)+1]:
                continue
            else:
                break
            ans = (i+1)*max(L//M,L//N)
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y <= 0:
        return x
    else:
        return gcd(y,x%y)

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

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

ans = -1
if N == M:
    if S == T:
        ans = N
    else:
        ans = -1
elif S[0] != T[0]:
    ans = -1
else:
    for i in range(1,N*M):
        for j in range(1,N*M):
            if i*(L//M)+1 < min(N,M) and j*(L//N)+1 < min(N,M) and T[i*(L//M)+1] == S[j*(L//N)+1]:
                continue
            else:
                break
            ans = (i+1)*max(L//M,L//N)
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y <= 0:
        return x
    else:
        return gcd(y,x%y)

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

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

ans = -1
if N == M:
    if S == T:
        ans = N
    else:
        ans = -1
elif S[0] != T[0]:
    ans = -1
else:
    for i in range(1,min(N,M)):
        for j in range(1,min(N,M)):
            if i*(L//M)+1 < min(N,M) and j*(L//N)+1 < min(N,M) and T[i*(L//M)+1] == S[j*(L//N)+1]:
                continue
            else:
                break
            ans = L
print(ans)

Submission status: Wrong Answer

Solution code:
def gcd(x,y):
    if y <= 0:
        return x
    else:
        return gcd(y,x%y)

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

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

ans = -1
if N == M:
    if S == T:
        ans = N
    else:
        ans = -1
elif S[0] != T[0]:
    ans = -1
else:
    for i in range(1,min(N,M)):
        for j in range(1,min(N,M)):
            if i*(L//M)+1 < min(N,M) and j*(L//N)+1 < min(N,M) and T[i*(L//M)+1] == S[j*(L//N)+1]:
                continue
            else:
                break
            ans = (i+1)*max(L//M,L//N)
print(ans)

Submission status: Wrong Answer

Solution code:
import fractions
n,m=map(int,input().split())
s=list(input())
t=list(input())
if s[0]!=t[0]:
    print(-1)
    exit()
if max(n,m)%min(n,m)!=0 and s[0]==t[0]:
    print(n*m//fractions.gcd(n,m))
    exit()
if n>m:
    k=s
    l=t
else:
    k=t
    l=s
f=len(k)//len(l)
for i in range(len(l)):
    if k[f*i]!=l[i]:
        print(-1)
        exit()
print(len(k))

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int, input().split())
S = input()
T = input()
lcm = (N*M) // fractions.gcd(N, M)
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
elif max(N, M) % min(N, M) == 0:
    if M > N:
        N, M = M, N
        S, T = T, S
    k = N // M
    for i in range(M):
        if S[i*k] != T[i]:
            print(-1)
            exit(0)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int, input().split())
S = input()
T = input()
lcm = (N*M) // fractions.gcd(N, M)
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
elif max(N, M) % min(N, M) == 0:
    if M > N:
        N, M = M, N
        S, T = T, S
    k = N // M
    for i in range(M):
        if S[i*k] != T[i]:
            print(-1)
            exit(0)
    print(lcm)

else:
    print(lcm)

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int, input().split())
S = input()
T = input()
if S[0] != T[0]:
    print(-1)
elif N == M and S != T:
    print(-1)
elif max(N, M) % min(N, M) == 0:
    if M > N:
        N, M = M, N
        S, T = T, S
    k = N // M
    for i in range(M):
        if S[i*k] != T[i]:
            print(-1)
            break

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


n,m = inpl()
s = input()
t = input()
a,b = max(n,m), min(n,m)
if a%b:
    if s[0] == t[0]:
        print(a*b//fractions.gcd(a,b))
    else:
        print(-1)
else:
    tmp = a//b
    for i in range(b):
        # print(i*tmp)
        if t[i] != s[i*tmp]:
            print(-1)
            break
    else:
        print(a)

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))


n,m = inpl()
s = input()
t = input()
a,b = max(n,m), min(n,m)
if a%b:
    if s[0] == t[0]:
        print(a*b//fractions.gcd(a,b))
    else:
        print(-1)
else:
    tmp = a//b
    for i in range(a):
        if s[i] != t[i*tmp]:
            print(-1)
            break
    else:
        print(a)

Submission status: Wrong Answer

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

for i in range(L):
  if S[i * div_N] != T[i * div_M]:
    print(-1)
    exit()
print(L)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def exist_good_str(N,M,S,T,L):
  X = {}
  for idx, s in enumerate(S):
    if idx != 0: idx = L//N*idx
    if idx in X and X[idx] != s: return False
    else: X[idx] = s
  
  for i in range(1,min(L,N),L//N):
    i -= 1
    X[i] = S[i]
  
  for idx, t in enumerate(T):
    if idx != 0: idx = L//M*idx 
    if idx in X and X[idx] != t: return False
    else: X[idx] = t
  return True
      
N,M = map(int,input().split())
S,T = input(), input()

L = int((N * M / gcd(N, M)))
if exist_good_str(N,M,S,T,L) is True: print(L)
else: print(-1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd

def exist_good_str(N,M,S,T,L):
  X = [0 for _ in range(L)]
  for idx, s in enumerate(S):
    if idx != 0: idx = L//N*idx
    if X[idx] != 0 and X[idx] != s: return False
    else: X[idx] = s
  
  for i in range(1,min(L,N),L//N):
    i -= 1
    X[i] = S[i]
  
  for idx, t in enumerate(T):
    if idx != 0: idx = L//M*idx 
    if X[idx] != 0 and X[idx] != t: return False
    else: X[idx] = t
  return True
      
N,M = map(int,input().split())
S,T = input(), input()


L = int((N * M / gcd(N, M)))
if exist_good_str(N,M,S,T,L) is True: print(L)
else: print(-1)

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
import bisect

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

def main():
    N,M = map(int,input().split())
    S = input().strip()
    T = input().strip()
    m = lcm(N,M)
    for i in range(m//M):
        si = i*m//M
        ti = i*m//N
        if si < N and ti < M and S[si] != T[ti]:
            print("-1")
            return
    print(m)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

f1=fractions.gcd(n,m)
f1=n*m//f1
if s[0]!=t[0]:
    print(-1)

elif max(n,m)%min(n,m)!=0:
  print(f1)
elif n==m:
    if s==t:
        print(n)
    else:
        print(-1)
elif n>m:
    f1=fractions.gcd(n,m)
    f1=n*m//f1
    
    check1=f1//n
    check2=f1//m
    for i in range(1,m-check1+1):
        if t[check1*i] != s[check2*i]:
            print(-1)
            exit()

    print(f1)
elif n<m:
    f1=fractions.gcd(n,m)
    f1=n*m//f1
    
    check1=f1//n
    check2=f1//m
    for i in range(1,n-check2+1):
        if t[check1*i] != s[check2*i]:
            print(-1)
            exit()

    print(f1)

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)
Slist = [[] for i in range(L)]
Tlist = [[] for i in range(L)]

for i in range(N):
  Slist[i*L//N] = S[i]
for i in range(M):
  Tlist[i*L//M] = T[i]
for i in range(L):
  if Slist[i] != [] or Tlist[i] != [] or Slist[i] == Tlist[i]:
    continue
  else:
    print(-1)
    exit()
#if Slist == Tlist:
#  print(L)
#else:
#  print(-1)
print(L)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(m>n):
		rate=m//n
		for i in range(0,n):
			if(s[i]!=t[i*rate]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(m)
	elif(n>m):
		rate=n//m
		for i in range(0,m):
			if(t[i]!=s[i*rate]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(n)
	else:
		print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(m>n):
		rate=m//n
		for i in range(0,n):
			if(s[i]!=t[i*rate]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(-1)
#		print(m)
	elif(n>m):
		rate=n//m
		for i in range(0,n):
			if(t[i]!=s[i*rate]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(-1)
#		print(n)
	else:
		print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(m>n):
		rate=m//n
		for i in range(0,n):
			if(s[i]!=t[i*rate]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(m)
	elif(n>m):
		rate=n//m
		for i in range(0,n):
			if(t[i]!=s[i*rate]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(n)
	else:
		print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(m>=n):
		rate=m//n
		for i in range(0,n):
			if(s[i]!=t[i*rate]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(m)
	else:
		rate=n//m
		for i in range(0,n):
			if(t[i]!=s[i*rate]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(n)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(n>=m):
		for i in range(0,n):
			if(s[i]!=t[i%m]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(n)
	else:
		for i in range(0,m):
			if(t[i]!=s[i%n]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(m)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
#AGC028A https://atcoder.jp/contests/agc028/tasks/agc028_a
import sys
#import collections

#n= int(input())
#s= input()
tmp = input().split()
n,m = list(map(lambda a: int(a), tmp))
s= input()
t= input()

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

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

tmp=lcm(n,m)
if(tmp!=n)and(tmp!=m):
	if(s[0]==t[0]):
		print(tmp)
	else:
#		print("hoge1")
		print(-1)
else:
	if(n>=m):
		for i in range(0,n):
			if(s[i]!=t[i%m]):
#				print("hoge2")
				print(-1)
				sys.exit()
		print(n)
	else:
		for i in range(0,m):
			if(t[i]!=s[i%n]):
#				print("hoge3")
				print(-1)
				sys.exit()
		print(n)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


N, M = map(int, input().split())
S, T = [input() for _ in range(2)]
L = gcd(N, M)
print(N*M//L if any([S[i*N//L] == T[i*M//L] for i in range(L)]) else -1)

Submission status: Wrong Answer

Solution code:
from fractions import gcd


n, m = map(int, input().split())
s = input()
t = input()
x = gcd(n, m)
l = n*m/x
for i in range(x):
    if s[i*(n//x)] != t[i*(m//x)]:
        print(-1)
        exit()
print(l)

Submission status: Wrong Answer

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

gcd = fractions.gcd(N,M)
for i in range(gcd):
    ans = True
    if S[(N//gcd)*i] != T[(M//gcd)*i]:
        ans = False

if ans:
    print(N*M//gcd)
else:
    print(-1)

Submission status: Wrong Answer

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

gcd = fractions.gcd(N,M)
for i in range(gcd):
    ans = True
    if S[(gcd//M)*i] != T[(gcd//N)*i]:
        ans = False

if ans:
    print(N*M//gcd)
else:
    print(-1)

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

a = lcm(N, M)
b = lcm(a//N, a//M)

l = [0]
tmp = 0
while tmp < a:
    tmp += b
    if tmp < a:
        l.append(tmp)

#print(a, b, l)

check = 0

for i in range(len(l)):
    if S[i//(a//N)] == T[i//(a//M)]:
        pass
    else:
        check = 1 

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

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

if gcd(N,M)==M or gcd(N,M)==M:
    ans=-1
else:
    ans=lcd(N,M)

print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit
import math
from fractions import gcd

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

N,M=map(int,input().split())
S=input()
T=input()
if S[0] != T[0]:
    print("-1")
    exit()
if N==M:
    if S==T:
        print(N)

    else:
        print(-1)
    exit()
    
if N<M:
    N,M=M,N
    S,T=T,S
    

# N>M S>T
if(N%M==0):
    S2=""
    for i in range(N):
        if i%(N//M)==0 :
            S2+=S[i]
            
    if S2==T:
        print(N)
    else:
        print(-1)
    exit()

else:
    print(lcm(N,M))

Submission status: Wrong Answer

Solution code:
from sys import exit
import math
from fractions import gcd

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

N,M=map(int,input().split())
S=input()
T=input()
if S[0] != T[0]:
    print("-1")
    exit()
if N<M:
    N,M=M,N
    S,T=T,S
    

# N>M S>T
if(N%M==0):
    S2=""
    for i in range(N):
        if i%(N//M)==0 :
            S2+=S[i]
            
    if S2==T:
        print(N)
    else:
        print(-1)
    exit()

else:
    print(lcm(N,M))

Submission status: Wrong Answer

Solution code:
from sys import exit
import math
from fractions import gcd

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

N,M=map(int,input().split())
S=input()
T=input()
if S[0] != T[0]:
    print("-1")
    exit()
if N<M:
    N,M=M,N
    S,T=T,S
    

# N>M S>T
if(N%M==0):
    S2=""
    for i in range(N):
        if i%(N//M)==0 :
            S2+=S[i]
            
    if S2==T:
        print(N)
    else:
        print(-1)
    exit()

else:
    print(lcm(N,M))

Submission status: Wrong Answer

Solution code:
from fractions import gcd

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

if N % M == 0 or M % N == 0:
    print(-1)
else:
    print((N * M) // gcd(N, M))

Submission status: Wrong Answer

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

def solve(n, m, s, t):
    if s[0] != t[0]:
        return -1
    if n < m:
        s, t = t, s
        n, m = m, n
    if n % m == 0:
        if s == t * (n // m):
            return n
        else:
            return -1
    else:
        return n * m // gcd(n, m)
n, m = map(int, input().split())
s = input()
t = input()
print(solve(n, m, s, t))

Submission status: Wrong Answer

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

def solve(n, m, s, t):
    if n < m:
        s, t = t, s
        n, m = m, n
    if n % m == 0:
        if s == t * (n // m):
            return n
        else:
            return -1
    else:
        return n * m // gcd(n, m)
n, m = map(int, input().split())
s = input()
t = input()
print(solve(n, m, s, t))