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
import sys

N,M=map(int,input().split())
S=input()
T=input()
S_len=len(S)
T_len=len(T)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

lcm_result=lcm(S_len,T_len)
S_data=[]
count=0
for i in range (lcm_result):
    if i == (lcm_result/N)*count:
        S_data.append(S[count])
        count+=1
    else:
        S_data.append(0)
T_data=[]
count=0
for i in range (lcm_result):
    if i == (lcm_result/M)*count:
        T_data.append(T[count])
        count+=1
    else:
        T_data.append(0)
for j in range (1,lcm_result):
    if S_data[j]!=0 and T_data[j]!=0 and S_data[j]!=T_data[j]:
        print (-1)
        sys.exit()
print(lcm_result)

Submission status: Wrong Answer

Solution code:
import fractions
N, M = map(int,input().split())
S = [x for x in input()]
T = [x for x in input()]

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

if N >= M:
    h, l, hl, ll = N, M, S, T
else:
    h, l, hl, ll = M, N, T, S

if h % l == 0:
    if h == l:
        if S == T:
            print(N * 2)
            exit()
    print(-1)
    exit()

else:
    print((h * l) // fractions.gcd(h, l))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import math
import fractions


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


def divisors(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list


if __name__ == '__main__':
    N, M = map(int, input().split(' '))
    S = input()
    T = input()

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

    L = lcm(N, M)

    m = max(N, M)
    m_divisors = divisors(m)
    m_divisors.remove(1)

    S = S * int(math.ceil(m / N))
    T = T * int(math.ceil(m / M))

    for i in [d - 1 for d in m_divisors]:
        if S[i] == T[i]:
            print(-1)
            exit(0)
    print(L)

Submission status: Wrong Answer

Solution code:

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

def AGC028A():
    N, M = map(int, input().split())
    S = input()
    T = input()
    if len(S) < len(T):
        S, T = T, S
    #if lcm(N, M) == max(N, M) and S[0:len(T)] == T:
     #   print(-1)
      #  return 0
    key = lcm(N, M)
    n = key // N
    m = key // M
    n_list = []
    for i in range(0, key, n):
        n_list.append(i)
    m_list = []
    for j in range(0, key, m):
        m_list.append(j)
    i = 0
    j = 0
    while True:
        if i >= len(n_list) or j >= len(m_list):
            break
        if i >= len(S) or j >= len(T):
            break
        if n_list[i] == m_list[j]:
            if S[i] != T[j]:
                print(-1)
                return 0
        if n_list[i] > m_list[j]:
            j += 1
        elif n_list[i] < m_list[j]:
            i += 1
        else:
            i += 1
            j += 1
    print(key)

AGC028A()

Submission status: Wrong Answer

Solution code:
def LCM(a, b):
    tmp = a * b
    while b != 0:
        a, b = b, a % b
    return tmp // a


N, M = map(int, input().split())
s = input()
t = input()
n = LCM(N, M)
a = n // N
b = n // M
for i in range(min((N - 1) // b, (n - 1) // a)):
    if s[b * i] != t[a * i]:
        print("-1")
        break

else:
    print(n)

Submission status: Wrong Answer

Solution code:
N, M =  map(int, input().split(' '))
S = input()
T = input()
import numpy as np
def gcd(a, b):
	while b:
		a, b = b, a % b
	return a
def lcm(x, y):
    return (x * y) // gcd(x, y)

L = lcm(N,M)

x1 = [1] + [(i *(L/N)) +1for i in range(1, N)] 
x2 = [1]+ [(i *(L/M)) +1for i in range(1, M)] 
x3 = set(x1) & set(x2) 

if x3 =={1}:
    print(L)
else :
    if all([S[np.where(np.array(x1)==i)[0][0]] == T[np.where(np.array(x2)==i)[0][0]] for i in x3]) :
        print(L)
    else :
        print(-1)

Submission status: Wrong Answer

Solution code:
import sys

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

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

if S[0] != T[0]:
  print('-1')
  sys.exit()
else:
  O = []
  O.append(S[0])

L = (N * M) // gcd(N, M)
LN = L // N
LM = L // M
rf = 0

for i in range(L-1):
  O.append('')

for i in range(1,N):
  O[(i*LN)] = S[i]

for i in range(1,M):
  if O[(i*LM)] == '' or T[i]:
    O[(i*LM)] = T[i]
  else:
    rf = 1
if rf == 0:
  print(L)
  sys.exit()
  
else:
  for i in range(3):
    L += (N * M) // gcd(N, M)
    LN = L // N
    LM = L // M
    rf = 0
    O = []
    for i in range(L):
      O.append('')
    O[0] = S[0]
    
    for i in range(1,N):
      O[(i*LN)] = S[i]

    for i in range(1,M):
      if O[(i*LM)] == '' or T[i]:
        O[(i*LM)] = T[i]
      else:
        rf = 1
    if rf == 0:
      print(L)
      sys.exit()
print('-1')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import math
import fractions


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


def divisors(num):
    if num < 1:
        return []
    elif num == 1:
        return [1]
    else:
        divisor_list = []
        divisor_list.append(1)
        for i in range(2, num // 2 + 1):
            if num % i == 0:
                divisor_list.append(i)
        divisor_list.append(num)

        return divisor_list


if __name__ == '__main__':
    N, M = map(int, input().split(' '))
    S = input()
    T = input()

    L = lcm(N, M)

    m = max(N, M)
    m_divisors = divisors(m)
    m_divisors.remove(1)

    S = S * int(math.ceil(m / N))
    T = T * int(math.ceil(m / M))

    for i in [d - 1 for d in m_divisors]:
        if S[i] == T[i]:
            print(-1)
            exit(0)
    print(L)

Submission status: Wrong Answer

Solution code:
import sys
n,m=map(int,input().split())
s=input()
t=input()
def g(a,b):
    while b:
        a,b=b,a%b
    return a
gcd=g(n,m)
l = n*m / gcd
slist=[1]
tlist=[1]
for i in range(1,n):
    j=i*(l/n)+1 
    jj=int(j)
    if j==jj:
        slist.append(j)
    k=i*(l/m) + 1
    kk=int(k)
    if k==kk:
        tlist.append(k)
slist=list(filter(lambda x: x <= l, slist))
tlist=list(filter(lambda x: x <= l, tlist))
se=[]
for i in range(int(l)):
    se.append(0)
for i in range(len(slist)):
    se[int(slist[i])-1] = s[i]
for i in range(len(tlist)):
    if se[int(tlist[i]) -1] != 0 and se[int(tlist[i]) -1] != t[i]:
        print(-1)
        sys.exit()
print(int(l))

Submission status: Wrong Answer

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

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

re = -1
# 1文字目が必ず一緒であること
if S[0] == T[0]:
    if N == M:
        if S == T:
            re = N
    else:
        min_num = (N * M)  // gcd(N, M) # 最小公倍数を求める（基本はこれが答え）
        re = min_num
        # 重なる部分が同じ文字かを調べる
        s_d = min_num // N
        t_d = min_num // M
        # 最小公倍数みる
        st_min = (s_d * t_d) // gcd(s_d, t_d)
        st = st_min
        i = 1
        while st // s_d < N and st // t_d < M:
            if S[st // s_d] != T[st // t_d]:
                re = -1
                break
            else:
                i += 1
                st = st * i

print(re)

Submission status: Wrong Answer

Solution code:
# from math import gcd
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 N > M:
  N, M = M, N
  S, T = T, S

i = 0

while True:
  i += 1
  L = i*lcm(N, M)
  N_list = [(L//N)*k + 1 for k in range(N)]
  M_list = [(L//M)*k + 1 for k in range(M)]

  print(N_list)
  print(M_list)

  memo = {}

  for idx, n in enumerate(N_list):
    memo[n] = S[idx]

  for idx, m in enumerate(M_list):
    if m in memo.keys():
      if memo[m] != T[idx]:
        break
  else:
    print(L)
    exit(0)

  # if L == N*M:
  break

print(-1)

Submission status: Wrong Answer

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

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

def enstr(A):
    S=''
    for i in A:
        S+=i
    return S
    
if S[0]==T[0]:
    gcd=gcd(N,M)
    if N<M:
        N,M=M,N
        S,T=T,S
    if gcd==M:
        if S==T*(N//M):
            print(N)
        else:
            print('-1')
    else:
        gcm=N*M//gcd
        print(gcm)

Submission status: Wrong Answer

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

N,M = map(int,input().split())
a = input()
b = input()

if N % M != 0 and M % N != 0:
    print(lcm(N,M))
elif N % M == 0:
    # a is the larger string
    step = N // M
    for i in range(M):
        if b[i] != a[i * step]:
            print(-1)
            quit()
    print(N)
else:
    # b is the larger string
    step = M // N
    for i in range(N):
        if a[i] != b[i * step]:
            print(-1)
            quit()
    print(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==(m+1)*3 and n!=m*2) else n[1] if ((n+1)*2==m*3) else -1)

Submission status: Wrong Answer

Solution code:
from fractions import *

def lcm(a,b): 
    return int(abs(a * b) / gcd(a,b) if a and b else 0)

N,M = map(int,input().split())
a = input()
b = input()

if N % M != 0 and M % N != 0:
    print(lcm(N,M))
elif N % M == 0:
    print("here")
    # a is the larger string
    step = N // M
    for i in range(M):
        if b[i] != a[i * step]:
            print(-1)
            quit()
    print(N)
else:
    # b is the larger string
    print("Here")
    step = M // N
    for i in range(N):
        if a[i] != b[i * step]:
            print(-1)
            quit()
    print(M)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=input()
slen=len(S)
T=input()
tlen=len(T)
ans=slen*tlen
if S[0]==T[0] and slen-tlen<5:
    print (ans)
else:
    print ("-1")

Submission status: Wrong Answer

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

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

re = -1
# 1文字目が必ず一緒であること
if S[0] == T[0]:
    min_num = (N * M)  // gcd(N, M) # 最小公倍数を求める（基本はこれが答え）
    re = min_num
    # 重なる部分が同じ文字かを調べる
    s_d = min_num // N
    t_d = min_num // M
    # 最小公倍数みる
    st_min = (s_d * t_d) // gcd(s_d, t_d)
    st = st_min
    i = 1
    while st // s_d < N and st // t_d < M:
        if S[st // s_d] != T[st // t_d]:
            re = -1
            break
        else:
            i += 1
            st = st * i

print(re)

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 gcd(N, M) == 1:
        if S[0] != T[0]:
            return -1
        return N * M
    else:
        c = gcd(N, M)
        for i in range(0, M, M//c):
            if S[i//c * N//c] != T[i]:
                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:
N,M = map(int,input().split(" "))
S = input()
T = input()

s = len(S)
t = len(T)

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

elif t >= s and len(T) % len(S) == 0:
    for i in range(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:
iN ,iM = [int(_) for _ in input().split()]
sS = input().rstrip()
sT = input().rstrip()

def fgcd(a,b):
    if b :
        return fgcd(b,a%b)
    else:
        return a

if sS[0] != sT[0]:
    print("-1")
    exit()
elif fgcd(iN,iM) == 1:
    print(iN*iM)
    exit()
else:
#iLX = iN * iM // fgcd(iN,iM)  の倍数
    iD = fgcd(iN,iM)
    i = 1
    while True:
        if iN -1  <  i * iM //iD   or iM -1  <  i*iN//iD :
            break
        if sS[i*iN//iD ] != sT[i*iM//iD  ] :
            print("-1")
            exit()
        i += 1
    print(iN*iM//iD)

Submission status: Wrong Answer

Solution code:
from sys import stdin
il = lambda func=int: list(map(func, stdin.readline().rstrip().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]
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)

n, m = il()
s = input()
t = input()
aa = gcd(n, m)
bb = lcm(n, m)
cc = aa // bb
if aa == 1:
    print(lcm(n, m))
    exit()
else:
    for i in range(aa):
        if s[n//aa*i] != t[m//aa*i]:
            print(-1)
            exit()
print(bb)

Submission status: Wrong Answer

Solution code:
import fractions

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

g = fractions.gcd(n, m)
l = int(n * m / g)
if l != n and l != m:
    print(l)
else:
    if n > m:
        res = n
        for i in range(m):
            if s[i * int(n / m)] == t[i]:
                pass
            else:
                res = -1
                break
    else:
        res = m
        for i in range(n):
            if t[i * int(m / n)] == s[i]:
                pass
            else:
                res = -1
                break
    print(res)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
s=input()
t=input()
def gcd (x,y):
    while 1:
        a=x%y
        if a==0:
            return y
        x=y
        y=a

d=gcd(n,m)
l=[]
for i in range(2,d+1):
    if d%i==0:
        l.append(i)
flag=0
if s[0]!=t[0]:
    flag=1
for i in l:
    if s[n//i]!=t[m//i]:
        flag=1
        break
if flag==1:
    print(-1)
else:
    print(n*m//d)

Submission status: Wrong Answer

Solution code:
# AGC 028 A
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)

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

L = lcm(N,M)
p = L//N
q = L//M
U = ""
k = 0
if N%M == 0:
    while k < N:
        U += S[k]
        k += q
    if T == U:
        print(N)
    else:
        print(-1)
elif M%N == 0:
    while k < M:
        U += T[k]
        k += p
    if S == U:
        print(M)
    else:
        print(-1)
else:
    print(L)

Submission status: Wrong Answer

Solution code:
import sys

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, l):
	if len(x) < len(y):
		x, y = y, x
	lx = int(l/len(x))
	ly = int(l/len(y))
	for i in range(len(x)):
		if i%lx == 0 and i%ly == 0 and x[int(i/lx)] != y[int(i/ly)]:
			return False
	return True

if __name__ == '__main__':
	N, M = list(map(int, input().split()))
	S = input()
	T = input()
	L = lcm(N,M)
	if not cmp(S,T,L):
		print(-1)
	else:
		print(L)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=input()
slen=len(S)
T=input()
tlen=len(T)
ans=slen*tlen
if S[0]=="a":
    print ("-1")
else:
  while tlen>0:
    tlen=tlen+1

Submission status: Wrong Answer

Solution code:
def Euclidean(a, b):
    if a<b:
        a, b = b, a
    while True:
        m = a%b
        if m==0 or b==1:
            return b
        a = b
        b = m

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

gcm = Euclidean(N, M)
gcd = int(N*M/gcm)
#print(gcm, gcd)

for i in range(min(N, M)):
    if S[i]!=T[i]:
        if i==0:
            print(-1)
        else:
            print(i*gcd)
        exit()
print(-1)

Submission status: Wrong Answer

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

def AGC028A():
    N, M = map(int, input().split())
    S = input()
    T = input()
    if len(S) < len(T):
        S, T = T, S
    #if lcm(N, M) == max(N, M) and S[0:len(T)] == T:
     #   print(-1)
      #  return 0
    key = lcm(N, M)
    n = key // N
    m = key // M
    n_list = []
    for i in range(0, key, n):
        n_list.append(i)
    m_list = []
    for j in range(0, key, m):
        m_list.append(j)
    i = 0
    j = 0
    while True:
        if i >= len(n_list) or j >= len(m_list):
            break
        if n_list[i] == m_list[j]:
            if S[i] != T[j]:
                print(-1)
                return 0
        if n_list[i] > m_list[j]:
            j += 1
        elif n_list[i] < m_list[j]:
            i += 1
        else:
            i += 1
            j += 1
    print(key)

AGC028A()

Submission status: Wrong Answer

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

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

re = -1
# 1文字目が必ず一緒であること
if S[0] == T[0]:
    min_num = (N * M)  // gcd(N, M) # 最小公倍数を求める（基本はこれが答え）
    re = min_num
    # 重なる部分が同じ文字かを調べる
    s_d = min_num // N
    t_d = min_num // M
    # 最小公倍数みる
    st_min = (s_d * t_d) // gcd(s_d, t_d)
    st = st_min
    i = 1
    while st < N and st < M:
        if S[st // (min_num // N)] != T[st // (min_num // M)]:
            re = -1
            break
        else:
            i += 1
            st = st * i

print(re)

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 (N)
    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 (M)
    else:
      print (-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from fractions import gcd
def inpl(): return list(map(int, input().split()))
N, M = inpl()
S = input()
T = input()

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

if gcd(N, M) == 1:
    print(G)

else:
    a = G//N
    b = G//M
    for x, y in zip(range(b, N+1, b), range(a, M+1, a)):
        if S[x-1] != T[y-1]:
            print(-1)
            break
    else:
        print(G)

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(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(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:
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)
else:
    d=lcm(m,n)
    f=max(m,n)%min(m,n)
    if f!=0:
        print(d)
    else:
        g=max(m,n)//min(m,n)
        if max(m,n)==m:
            k=t
            q=s
            l=n
        else:
            k=s
            q=t
            l=m
        for i in range(0,l):
            if k[g*i]!=q[i]:
                print(-1)
                break
        else:
            print(m+n-l)

Submission status: Wrong Answer

Solution code:
N, M = input().split()
S=input()
slen=len(S)
T=input()
tlen=len(T)
ans=slen*tlen
if S[0]=="a":
    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==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[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)

Submission status: Wrong Answer

Solution code:
import fractions
import sys

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)
    sys.exit()

f = lcm(len(S), len(T))

if min(len(S), len(T)) == 1:
    print(1)
    sys.exit()

for i in range(1, min(len(S), len(T))):
    if S[i] == T[i]:
        print(-1)
        sys.exit()

print(f)

Submission status: Wrong Answer

Solution code:
ai = lambda: list(map(int,input().split()))
ai_ = lambda: [int(x)-1 for x in 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:
        i = 0
        while True:
            i += 1
        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 s == t:
    if S == T:
        print("-1")
    else:
        print(s)
    exit(0)

elif len(S) % len(T) == 0:
    for i in range(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(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:
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

def AGC028A():
    N, M = map(int, input().split())
    S = input()
    T = input()
    if len(S) < len(T):
        S, T = T, S
    if lcm(N, M) == max(N, M) and S[0:len(T)] == T:
        print(-1)
        return 0
    key = lcm(N, M)
    n = key // N
    m = key // M
    n_list = []
    for i in range(0, key, n):
        n_list.append(i)
    m_list = []
    for j in range(0, key, m):
        m_list.append(j)
    i = 0
    j = 0
    while True:
        if i >= len(n_list) or j >= len(m_list):
            break
        if n_list[i] == m_list[j]:
            if S[i] != T[j]:
                print(-1)
                return 0
        if n_list[i] > m_list[j]:
            j += 1
        elif n_list[i] < m_list[j]:
            i += 1
        else:
            i += 1
            j += 1
    print(key)

AGC028A()

Submission status: Wrong Answer

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

def _gcd(a,b):
  while b>0:
    a,b=b,a%b
  return a

gcd = _gcd(N,M)
lcm = N*M//gcd

def check():
  a,b = 0,0
  da,db = N//gcd, M//gcd
  while a < N:
    if S[a]!=T[b]:
      print(a, S[a])
      print(b, T[b])
      print(-1)
      return
    a+=da
    b+=db
  print(lcm)

check()

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:
	g = gcd(n, m)
	lcm = n // g * m
	if (lcm // n) // gcd(lcm // n, lcm // m) * (lcm // m) == lcm:
		#print("hoge")
		print(n // g * m)
	else:
		p = (lcm // n) // gcd(lcm // n, lcm // m) * (lcm // m)
		#print(p)
		ok = True
		for i in range(lcm // p):
			#print(s[(lcm // m) * i], t[(lcm // n) * i])
			if s[(lcm // m) * i] != t[(lcm // n) * i]:
				ok = False
				break
		if ok:
			print(n // g * m)
		else:
			print(-1)
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 = input().split()
S=input()
slen=len(S)
T=input()
tlen=len(T)
ans=slen*tlen
if S[0]==T[0]:
    print (ans)
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>0:
        a,b = b,a%b
    return a

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

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

s = [S[i*m] for i in range(G)]
t = [T[i*n] for i in range(G)]

if s == t or G == 1:
    print(L)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
ai = lambda: list(map(int,input().split()))
ai_ = lambda: [int(x)-1 for x in 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(0)

Submission status: Wrong Answer

Solution code:
import sys

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

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

if S[0] != T[0]:
  print('-1')
  sys.exit()
else:
  O = []
  O.append(S[0])

L = (N * M) // gcd(N, M)
LN = L // N
LM = L // M
rf = 0

for i in range(L-1):
  O.append('')

for i in range(1,N):
  O[(i*LN)] = S[i]

for i in range(1,M):
  if O[(i*LM)] == '' or T[i]:
    O[(i*LM)] = T[i]
  else:
    rf = 1
if rf == 0:
  print(L)
  sys.exit()
  
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 and n!=m*2) 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

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

if x != N and x != M:
    print(x)
else:
    if N == M:
        if S == T:
            print(x)
        else:
            print(-1)
    elif N > M:
        for index, i in enumerate(S):
            if index % int(N // M) == 0:
                if T[int(index / int(N // M))] != i:
                    break
        else:
            print(x)
            exit()
    else:
        for index, i in enumerate(T):
            if index % int(M // N) == 0:
                if S[int(index / int(M // N))] != i:
                    break
        else:
            print(x)
            exit()

    print(-1)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from fractions import gcd
def inpl(): return list(map(int, input().split()))
N, M = inpl()
S = input()
T = input()

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

if gcd(N, M) == 1:
    print(G)

else:
    a = G//N
    b = G//M
    for x, y in zip(range(0, N, b), range(0, M, a)):
        if S[x] != T[y]:
            print(-1)
            break
    else:
        print(G)

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

except:
    print(-1)
    exit()

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)

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[0]!=t[0]:
        print(-1)
        sys.exit()
    print(n*m//x)

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(1,d+1,max(a)):
    if i == 1:
        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:
    if d != max(a):
        print(d)
    else:
        print(-1)
else:
    print(-1)